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

          雪山飛鵠

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

          BlogJava 首頁(yè) 新隨筆 聯(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)建一個(gè)普通的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ā)布和調(diào)用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>
          客戶端調(diào)用
          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>
          客戶端調(diào)用
          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/  來(lái)源于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根據(jù)wsdl文件動(dòng)態(tài)調(diào)用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 為接口中定義的方法名稱   張三為傳遞的參數(shù)   返回一個(gè)Object數(shù)組
                  Object[] objects=client.invoke("sayHello""張三"); 
                  
          //輸出調(diào)用結(jié)果
                  System.out.println(objects[0].toString());
              }
          }
          下載工程代碼
          posted on 2010-09-15 11:18 雪山飛鵠 閱讀(36285) 評(píng)論(12)  編輯  收藏 所屬分類: webservice

          Feedback

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

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

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

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

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

          # re: 使用CXF發(fā)布和調(diào)用webservice之HelloWorld入門 2012-12-07 15:09 啊啊啊
          會(huì)報(bào)這個(gè)錯(cuò)誤~
          十二月 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)  回復(fù)  更多評(píng)論
            

          # re: 使用CXF發(fā)布和調(diào)用webservice之HelloWorld入門 2013-03-15 15:50 dbdebuger
          @啊啊啊

          原因是你需要使用java安裝目錄下面的JDK里面的jre  回復(fù)  更多評(píng)論
            

          # re: 使用CXF發(fā)布和調(diào)用webservice之HelloWorld入門 2014-05-26 17:33 老王頭
          經(jīng)過(guò)驗(yàn)證,這個(gè)確實(shí)好用。調(diào)用部署在AWS上的webservice成功。2014/05/26  回復(fù)  更多評(píng)論
            

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

          # Hello[未登錄](méi) 2015-06-08 15:39 張三
          Good  回復(fù)  更多評(píng)論
            

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

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

          主站蜘蛛池模板: 张家川| 定陶县| 台安县| 乌拉特后旗| 罗源县| 甘孜| 鄯善县| 嵩明县| 女性| 山东省| 临泽县| 潍坊市| 永兴县| 马公市| 农安县| 柘城县| 泸水县| 湾仔区| 正镶白旗| 乡城县| 罗平县| 梅州市| 阿坝县| 当雄县| 仪征市| 北川| 大埔区| 汝城县| 万安县| 大丰市| 靖远县| 永嘉县| 贵南县| 德昌县| 徐汇区| 常宁市| 平山县| 灵石县| 东莞市| 特克斯县| 沁源县|