<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.tkk7.com/sxyx2008/謝謝合作!!!

    雪山飛鵠

    溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.tkk7.com/sxyx2008/謝謝合作!!!

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks

    依賴的JAR
        cxf-2.2.10.jar
        jetty-6.1.21.jar
        jetty-util-6.1.21.jar
        servlet-2_5-api.jar
        wsdl4j-1.6.2.jar
        XmlSchema-1.4.5.jar
    創建一個普通的Java工程即可

    創建webservice接口
    package com.cxf.interfaces;

    import javax.jws.WebParam;
    import javax.jws.WebService;

    @WebService
    public interface HelloWorldServiceInf {
        
        String sayHello(@WebParam(name
    ="username") String username);
        
    }
    發布和調用webservice
            方法一
    發布webservice
    package com.cxf.impl;

    import javax.jws.WebService;

    import org.apache.cxf.interceptor.LoggingInInterceptor;
    import org.apache.cxf.interceptor.LoggingOutInterceptor;
    import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

    import com.cxf.interfaces.HelloWorldServiceInf;

    @WebService(endpointInterface
    ="com.cxf.interfaces.HelloWorldServiceInf",serviceName="helloWorldService")
    public class Server implements HelloWorldServiceInf {

        
    public String sayHello(String username) {
            
    return "Hello,"+username;
        }

        
        
    public static void main(String[] args) {
            Server impl
    =new Server();
            JaxWsServerFactoryBean factoryBean
    =new JaxWsServerFactoryBean();
            factoryBean.setAddress(
    "http://localhost:9000/hello");
            factoryBean.setServiceClass(HelloWorldServiceInf.
    class);
            factoryBean.setServiceBean(impl);
            factoryBean.getInInterceptors().add(
    new LoggingInInterceptor());
            factoryBean.getOutInterceptors().add(
    new LoggingOutInterceptor());
            factoryBean.create();
        }
        
    }
    wsdl描述文件
      <?xml version="1.0" ?> 
    <wsdl:definitions name="HelloWorldServiceInfService" targetNamespace="http://interfaces.cxf.com/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://interfaces.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <wsdl:types>
    <xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://interfaces.cxf.com/" xmlns:tns="http://interfaces.cxf.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      
    <xsd:element name="sayHello" type="tns:sayHello" /> 
    <xsd:complexType name="sayHello">
    <xsd:sequence>
      
    <xsd:element minOccurs="0" name="username" type="xsd:string" /> 
      
    </xsd:sequence>
      
    </xsd:complexType>
      
    <xsd:element name="sayHelloResponse" type="tns:sayHelloResponse" /> 
    <xsd:complexType name="sayHelloResponse">
    <xsd:sequence>
      
    <xsd:element minOccurs="0" name="return" type="xsd:string" /> 
      
    </xsd:sequence>
      
    </xsd:complexType>
      
    </xsd:schema>
      
    </wsdl:types>
    <wsdl:message name="sayHelloResponse">
      
    <wsdl:part element="tns:sayHelloResponse" name="parameters" /> 
      
    </wsdl:message>
    <wsdl:message name="sayHello">
      
    <wsdl:part element="tns:sayHello" name="parameters" /> 
      
    </wsdl:message>
    <wsdl:portType name="HelloWorldServiceInf">
    <wsdl:operation name="sayHello">
      
    <wsdl:input message="tns:sayHello" name="sayHello" /> 
      
    <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" /> 
      
    </wsdl:operation>
      
    </wsdl:portType>
    <wsdl:binding name="HelloWorldServiceInfServiceSoapBinding" type="tns:HelloWorldServiceInf">
      
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
    <wsdl:operation name="sayHello">
      
    <soap:operation soapAction="" style="document" /> 
    <wsdl:input name="sayHello">
      
    <soap:body use="literal" /> 
      
    </wsdl:input>
    <wsdl:output name="sayHelloResponse">
      
    <soap:body use="literal" /> 
      
    </wsdl:output>
      
    </wsdl:operation>
      
    </wsdl:binding>
    <wsdl:service name="HelloWorldServiceInfService">
    <wsdl:port binding="tns:HelloWorldServiceInfServiceSoapBinding" name="HelloWorldServiceInfPort">
      
    <soap:address location="http://localhost:9000/hello" /> 
      
    </wsdl:port>
      
    </wsdl:service>
      
    </wsdl:definitions>
    客戶端調用
    package com.cxf.client;

    import org.apache.cxf.interceptor.LoggingInInterceptor;
    import org.apache.cxf.interceptor.LoggingOutInterceptor;
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
    import com.cxf.interfaces.HelloWorldServiceInf;

    public class Client {
        
    public static void main(String[] args) {
            JaxWsProxyFactoryBean  factoryBean
    =new JaxWsProxyFactoryBean();
            factoryBean.getInInterceptors().add(
    new LoggingInInterceptor());
            factoryBean.getOutInterceptors().add(
    new LoggingOutInterceptor());
            factoryBean.setServiceClass(HelloWorldServiceInf.
    class);
            factoryBean.setAddress(
    "http://localhost:9000/hello");
            HelloWorldServiceInf impl
    =(HelloWorldServiceInf) factoryBean.create();
            System.out.println(impl.sayHello(
    "張三"));
        }
    }
            方法二
    發布webservice
    package com.cxf.impl;

    import javax.jws.WebService;
    import javax.xml.ws.Endpoint;

    import com.cxf.interfaces.HelloWorldServiceInf;

    @WebService(endpointInterface
    ="com.cxf.interfaces.HelloWorldServiceInf",serviceName="helloWorldService")
    public class Server implements HelloWorldServiceInf {

        
    public String sayHello(String username) {
            
    return "Hello,"+username;
        }
        
    public static void main(String[] args) {
            Server impl
    =new Server();
            String address
    ="http://localhost:9000/hello";
            Endpoint.publish(address, impl);
        }
    }
    wsdl文件
      <?xml version="1.0" ?> 
    <wsdl:definitions name="helloWorldService" targetNamespace="http://impl.cxf.com/" xmlns:ns1="http://interfaces.cxf.com/" xmlns:ns2="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      
    <wsdl:import location="http://localhost:9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace="http://interfaces.cxf.com/" /> 
    <wsdl:binding name="helloWorldServiceSoapBinding" type="ns1:HelloWorldServiceInf">
      
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
    <wsdl:operation name="sayHello">
      
    <soap:operation soapAction="" style="document" /> 
    <wsdl:input name="sayHello">
      
    <soap:body use="literal" /> 
      
    </wsdl:input>
    <wsdl:output name="sayHelloResponse">
      
    <soap:body use="literal" /> 
      
    </wsdl:output>
      
    </wsdl:operation>
      
    </wsdl:binding>
    <wsdl:service name="helloWorldService">
    <wsdl:port binding="tns:helloWorldServiceSoapBinding" name="ServerPort">
      
    <soap:address location="http://localhost:9000/hello" /> 
      
    </wsdl:port>
      
    </wsdl:service>
      
    </wsdl:definitions>
    客戶端調用
    package com.cxf.client;

    import javax.xml.namespace.QName;
    import javax.xml.ws.Service;
    import javax.xml.ws.soap.SOAPBinding;

    import com.cxf.interfaces.HelloWorldServiceInf;

    public class Client {
        
    //注意:此處http://interfaces.cxf.com/  來源于wsdl文件中namespace   <wsdl:import location="http://localhost:9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace="http://interfaces.cxf.com/" /> 

        
    private static final QName SERVICE_NAME=new QName("http://interfaces.cxf.com/","HelloWorldServiceInf");//HelloWorldServiceInf接口類的名稱
        private static final QName PORT_NAME=new QName("http://interfaces.cxf.com/""HelloWorldServiceInfPort");//HelloWorldServiceInfPort 接口類的名稱+Port
        public static void main(String[] args) {
            String endPointAddress
    ="http://localhost:9000/hello";
            Service service
    =Service.create(SERVICE_NAME);
            service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endPointAddress);
            HelloWorldServiceInf inf
    =service.getPort(HelloWorldServiceInf.class);
            System.out.println(inf.sayHello(
    "張三"));
        }
    }
    CXF根據wsdl文件動態調用WebService
    package com.cxf.client;

    import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

    public class ClientFromWsdl {
        
        
    public static void main(String[] args) throws Exception{
            JaxWsDynamicClientFactory dcf 
    = JaxWsDynamicClientFactory.newInstance();
            org.apache.cxf.endpoint.Client client 
    = dcf.createClient("http://localhost:9000/hello?wsdl");
            
    //sayHello 為接口中定義的方法名稱   張三為傳遞的參數   返回一個Object數組
            Object[] objects=client.invoke("sayHello""張三"); 
            
    //輸出調用結果
            System.out.println(objects[0].toString());
        }
    }
    下載工程代碼
    posted on 2010-09-15 11:18 雪山飛鵠 閱讀(36281) 評論(12)  編輯  收藏 所屬分類: webservice

    Feedback

    # re: 使用CXF發布和調用webservice之HelloWorld入門 2010-10-18 17:20 西木頭
    謝謝你?。】吹絺€寫這么清楚的額真不容易。。。  回復  更多評論
      

    # re: 使用CXF發布和調用webservice之HelloWorld入門 2010-10-18 18:02 西木頭
    哎 又有新問題了 別人給的是WSDL 客戶端CXF寫的話怎么個步驟啊  回復  更多評論
      

    # re: 使用CXF發布和調用webservice之HelloWorld入門 2010-10-20 16:05 鄧志容
    @西木頭
    我覺得有2中簡單用法:
    1、用cxf包中的wsdl2java來生成java代碼。關于cxf中的wsdl2java用法,你在網上查下,很多介紹。
    2、就是博主的最后一個方法,我試了下,用起來非常簡單。  回復  更多評論
      

    # re: 使用CXF發布和調用webservice之HelloWorld入門 2012-11-15 17:23 paco fan
    非常感謝,幫了我大忙了!  回復  更多評論
      

    # re: 使用CXF發布和調用webservice之HelloWorld入門[未登錄] 2012-12-05 15:15 aaaa
    cxf-2.2.10.jar包都過期了,2.7.0環境的教程有么?
      回復  更多評論
      

    # re: 使用CXF發布和調用webservice之HelloWorld入門 2012-12-07 15:09 啊啊啊
    會報這個錯誤~
    十二月 07, 2012 3:09:06 下午 org.apache.cxf.common.jaxb.JAXBUtils logGeneratedClassNames
    INFO: Created classes: com.cxf.interfaces.ObjectFactory, com.cxf.interfaces.SayHello, com.cxf.interfaces.SayHelloResponse
    java.lang.NullPointerException
    at org.apache.cxf.common.util.Compiler.useJava6Compiler(Compiler.java:189)
    at org.apache.cxf.common.util.Compiler.compileFiles(Compiler.java:143)
    at org.apache.cxf.common.util.Compiler.compileFiles(Compiler.java:138)
    at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.compileJavaSrc(DynamicClientFactory.java:598)
    at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:367)
    at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:235)
    at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:228)
    at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:183)
    at com.cxf.client.ClientFromWsdl.main(ClientFromWsdl.java:11)  回復  更多評論
      

    # re: 使用CXF發布和調用webservice之HelloWorld入門 2013-03-15 15:50 dbdebuger
    @啊啊啊

    原因是你需要使用java安裝目錄下面的JDK里面的jre  回復  更多評論
      

    # re: 使用CXF發布和調用webservice之HelloWorld入門 2014-05-26 17:33 老王頭
    經過驗證,這個確實好用。調用部署在AWS上的webservice成功。2014/05/26  回復  更多評論
      

    # re: 使用CXF發布和調用webservice之HelloWorld入門 2014-11-06 17:03 淡淡道
    @dbdebuger
    123  回復  更多評論
      

    # Hello[未登錄] 2015-06-08 15:39 張三
    Good  回復  更多評論
      

    # re: 使用CXF發布和調用webservice之HelloWorld入門 2015-09-24 11:24 aabbcc
    @張三
    警告: [options] 未與 -source 1.5 一起設置引導類路徑
    著是啥問題呢,用的動態調用  回復  更多評論
      

    # re: 使用CXF發布和調用webservice之HelloWorld入門[未登錄] 2016-04-20 15:11 薛勇
    @鄧志容
    我使用動態客戶端的時候報錯了:Created classes: com.xy.cxf.ws.ObjectFactory, com.xy.cxf.ws.SayHello, com.xy.cxf.ws.SayHelloResponse
    org.apache.cxf.common.i18n.UncheckedException: No operation was found with the name {http://impl.ws.cxf.xy.com/}sayHello.
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:347)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:341)
    at com.cxf.client.JaxWsDynamicClient.main(JaxWsDynamicClient.java:12)
    怎么破?  回復  更多評論
      

    主站蜘蛛池模板: MM1313亚洲精品无码久久| 亚洲日本韩国在线| 亚洲人成网国产最新在线| 97在线视频免费公开观看| 99人中文字幕亚洲区| 一级毛片免费视频| 亚洲人成影院在线| 最近2019免费中文字幕6| 久久国产亚洲精品无码| 91精品免费观看| 亚洲理论片在线观看| 亚洲AV无码资源在线观看| 成年男女男精品免费视频网站| 亚洲国产视频久久| 日韩精品视频免费在线观看| 亚洲av无码片vr一区二区三区| 国产免费怕怕免费视频观看| 亚洲日韩乱码中文无码蜜桃 | 久久久久免费精品国产小说| 久久亚洲AV午夜福利精品一区| 99久9在线|免费| 亚洲一级毛片免观看| 成人啪精品视频免费网站| 日本亚洲高清乱码中文在线观看| 亚洲?v女人的天堂在线观看| 亚洲日韩久久综合中文字幕| 日韩一级免费视频| 午夜在线免费视频| 国产成人免费a在线视频色戒| 国产精品亚洲五月天高清| 久久久无码精品亚洲日韩软件 | 亚洲日本人成中文字幕| 67pao强力打造国产免费| 亚洲av午夜成人片精品网站| 18级成人毛片免费观看| 亚洲一区二区无码偷拍| 西西大胆无码视频免费| 亚洲成A∨人片在线观看无码| 成人免费福利电影| 四虎影视久久久免费| 久久亚洲伊人中字综合精品|