转自:https://www.aliyun.com/zixun/wenji/1263190.html

CXF服务端:

[java] view plain copy
 
  1. package com.sean.server;  
  2.   
  3. import javax.jws.WebParam;  
  4. import javax.jws.WebService;  
  5.   
  6. @WebService  
  7. public interface Plus {  
  8.     public int add(@WebParam(name="x") int x, @WebParam(name="y") int y);  
  9. }  
[java] view plain copy
 
  1. package com.sean.server;  
  2.   
  3. public class PlusImpl implements Plus {  
  4.     public int add(int x, int y){  
  5.         return x + y;  
  6.     }  
  7. }  
[java] view plain copy
 
  1. package com.sean.server;  
  2.   
  3. import org.apache.cxf.jaxws.JaxWsServerFactoryBean;  
  4. import org.apache.cxf.frontend.ServerFactoryBean;  
  5.   
  6. public class Server {  
  7.     public static void main(String args[]) throws Exception {  
  8.         PlusImpl plusImpl = new PlusImpl();  
  9.           
  10.         JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();  
  11.         //ServerFactoryBean factory = new ServerFactoryBean();  
  12.         factory.setServiceClass(Plus.class);  
  13.         factory.setAddress("http://127.0.0.1:8888/Plus");  
  14.         factory.setServiceBean(plusImpl);  
  15.         factory.create();  
  16.     }  
  17. }  

程序启动后,访问http://127.0.0.1:8888/Plus?wsdl即可查看自动生成的WSDL文件

[html] view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <wsdl:definitions targetNamespace="http://server.sean.com/"   
  3.         name="PlusService" xmlns:ns1="http://schemas.xmlsoap.org/soap/http"   
  4.         xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"   
  5.         xmlns:tns="http://server.sean.com/"   
  6.         xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"   
  7.         xmlns:xsd="http://www.w3.org/2001/XMLSchema">   
  8.     <wsdl:types>   
  9.         <xs:schema targetNamespace="http://server.sean.com/"   
  10.                 xmlns:tns="http://server.sean.com/" version="1.0"   
  11.                 elementFormDefault="unqualified"   
  12.                 xmlns:xs="http://www.w3.org/2001/XMLSchema">   
  13.             <xs:element name="add" type="tns:add"/>   
  14.             <xs:element name="addResponse" type="tns:addResponse"/>   
  15.             <xs:complexType name="add">   
  16.                 <xs:sequence>   
  17.                     <xs:element name="x" type="xs:int"/>   
  18.                     <xs:element name="y" type="xs:int"/>   
  19.                 </xs:sequence>   
  20.             </xs:complexType>   
  21.             <xs:complexType name="addResponse">   
  22.                 <xs:sequence>   
  23.                     <xs:element name="return" type="xs:int"/>   
  24.                 </xs:sequence>   
  25.             </xs:complexType>   
  26.         </xs:schema>   
  27.     </wsdl:types>   
  28.     <wsdl:message name="addResponse">   
  29.         <wsdl:part name="parameters" element="tns:addResponse"</wsdl:part>   
  30.     </wsdl:message>   
  31.     <wsdl:message name="add">   
  32.         <wsdl:part name="parameters" element="tns:add"</wsdl:part>   
  33.     </wsdl:message>   
  34.     <wsdl:portType name="Plus">   
  35.         <wsdl:operation name="add">   
  36.             <wsdl:input name="add" message="tns:add"</wsdl:input>   
  37.             <wsdl:output name="addResponse" message="tns:addResponse"</wsdl:output>   
  38.         </wsdl:operation>   
  39.     </wsdl:portType>   
  40.     <wsdl:binding name="PlusServiceSoapBinding" type="tns:Plus">   
  41.         <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>   
  42.         <wsdl:operation name="add">   
  43.             <soap:operation style="document" soapAction=""/>   
  44.             <wsdl:input name="add">   
  45.                 <soap:body use="literal"/>   
  46.             </wsdl:input>   
  47.             <wsdl:output name="addResponse">   
  48.                 <soap:body use="literal"/>   
  49.             </wsdl:output>   
  50.         </wsdl:operation>   
  51.     </wsdl:binding>   
  52.     <wsdl:service name="PlusService">   
  53.         <wsdl:port name="PlusPort" binding="tns:PlusServiceSoapBinding">   
  54.             <soap:address location="http://127.0.0.1:8888/Plus"/>   
  55.         </wsdl:port>   
  56.     </wsdl:service>   
  57. </wsdl:definitions>  

如果服务端使用ServerFactoryBean类,则最终生成的WSDL文件略有不同
 

CXF客户端:

如果服务端使用ServerFactoryBean类,则客户端需要使用JaxWsServerFactoryBean类

如果服务端使用JaxWsServerFactoryBean类,则客户端需要使用JaxWsProxyFactoryBean类

[java] view plain copy
 
  1. package com.sean.client;  
  2.   
  3. import org.apache.cxf.frontend.ClientProxyFactoryBean;  
  4. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  5.   
  6. import com.sean.server.Plus;  
  7.   
  8. public class Client {  
  9.     public static void main(String[] args) {  
  10.         //ClientProxyFactoryBean factory = new ClientProxyFactoryBean();  
  11.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
  12.         factory.setAddress("http://127.0.0.1:8888/Plus");  
  13.         Plus client = factory.create(Plus.class);  
  14.         System.out.println(client.add(2, 2));  
  15.         System.exit(0);  
  16.     }  
  17. }  

无论服务端使用ServerFactoryBean类还是JaxWsServerFactoryBean类,都可在客户端使用JaxWsDynamicClientFactory类,并通过反射的方式调用WebService服务端

[java] view plain copy
 
  1. package com.sean.client;  
  2.   
  3. import org.apache.cxf.endpoint.Client;  
  4. import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;  
  5.   
  6. public class Client2 {  
  7.     public static void main(String[] args) throws Exception {  
  8.         JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();  
  9.         Client client = factory.createClient("http://127.0.0.1:8888/Plus?wsdl");  
  10.         Object[] inputs = {1, 2};  
  11.         Object[] result = client.invoke("add", inputs);  
  12.         System.out.println(result[0]);  
  13.     }  
  14. }  

 

posted on 2018-01-29 11:13  Sharpest  阅读(338)  评论(0编辑  收藏  举报