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

          雪山飛鵠

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

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            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
          創(chuàng)建一個普通的Java工程即可

          創(chuàng)建webservice接口
          package com.cxf.interfaces;

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

          @WebService
          public interface HelloWorldServiceInf {
              
              String sayHello(@WebParam(name
          ="username") String username);
              
          }
          發(fā)布和調用webservice
                  方法一
          發(fā)布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(
          "張三"));
              }
          }
                  方法二
          發(fā)布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文件動態(tài)調用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 雪山飛鵠 閱讀(36286) 評論(12)  編輯  收藏 所屬分類: webservice

          Feedback

          # re: 使用CXF發(fā)布和調用webservice之HelloWorld入門 2010-10-18 17:20 西木頭
          謝謝你啊!看到個寫這么清楚的額真不容易。。。  回復  更多評論
            

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

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

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

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

          # re: 使用CXF發(fā)布和調用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發(fā)布和調用webservice之HelloWorld入門 2013-03-15 15:50 dbdebuger
          @啊啊啊

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

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

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

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

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

          # re: 使用CXF發(fā)布和調用webservice之HelloWorld入門[未登錄] 2016-04-20 15:11 薛勇
          @鄧志容
          我使用動態(tài)客戶端的時候報錯了: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)
          怎么破?  回復  更多評論
            

          主站蜘蛛池模板: 德惠市| 邹平县| 正宁县| 西和县| 乌拉特后旗| 石泉县| 丹凤县| 嫩江县| 永仁县| 马龙县| 岢岚县| 万源市| 香港| 汪清县| 禄丰县| 宜州市| 体育| 东城区| 望谟县| 盐边县| 韶山市| 福安市| 桃园县| 西宁市| 东平县| 永靖县| 阳朔县| 阳泉市| 威信县| 文登市| 镇康县| 林甸县| 徐闻县| 金昌市| 三河市| 新田县| 云龙县| 康平县| 垦利县| 荆门市| 乐业县|