5、Web Service-整合CXF
阿新 • • 發佈:2019-03-21
ack utili sta log 請求 pes tns urn div
1、工程準備
繼續使用之前的服務端:https://www.cnblogs.com/Mrchengs/p/10562458.html
2、jar準備
前去apache官網下載響應的jar:http://cxf.apache.org/download.html
3、在原來的工程中導入jar文件
其中提供的jar相對比較多可以根據開發需求去導入相應的jar!
啟動服務:
可以看到使用的是jetty服務的
4、查看wsdl
http://localhost:8081/webserviceserver/helloService?wsdl
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.cr.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://service.cr.com/" name="HelloServiceImplService" targetNamespace="http://impl.service.cr.com/"> <wsdl:import location="http://localhost:8081/webserviceserver/helloService?wsdl=HelloService.wsdl" namespace="http://service.cr.com/"> </wsdl:import> <wsdl:binding name="HelloServiceImplServiceSoapBinding" type="ns1:HelloService"> <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="HelloServiceImplService"> <wsdl:port binding="tns:HelloServiceImplServiceSoapBinding" name="HelloServiceImplPort"> <soap:address location="http://localhost:8081/webserviceserver/helloService"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
創建新的工程,將其拷貝到新的wsdl文件中進行編譯
進行編譯使用
環境變量的配置:https://www.cnblogs.com/ChrisMurphy/p/5224160.html
執行命令:
工程目錄
新建測試類:
5、測試類
package cn.com.client; import com.cr.service.HelloService; import com.cr.service.impl.HelloServiceImplService; public class client { public static void main(String[] args){ HelloServiceImplService factory = new HelloServiceImplService(); HelloService hello = factory.getHelloServiceImplPort(); String res = hello.sayHello("mr"); System.out.println(res); } }
得到的結果如下:
6、分析請求和響應
request
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:q0="http://service.cr.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <q0:sayHello> <arg0>br</arg0> </q0:sayHello> </soapenv:Body> </soapenv:Envelope>
response
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:sayHelloResponse xmlns:ns2="http://service.cr.com/"> <return>hello:br</return> </ns2:sayHelloResponse> </soap:Body> </soap:Envelope>
分析:
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.cr.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.service.cr.com/" name="HelloServiceImplService"> <!-- types schema : 定義了一些標簽結構 --> <types> <xsd:schema> <xsd:import namespace="http://impl.service.cr.com/" schemaLocation="http://localhost:8081/webserviceserver/helloService?xsd=1"></xsd:import> </xsd:schema> </types> <!-- message: 用來定義消息的結構 soap消息 part : 指定引用types中定義的標簽片斷 --> <message name="sayHello"> <part name="parameters" element="tns:sayHello"></part> </message> <message name="sayHelloResponse"> <part name="parameters" element="tns:sayHelloResponse"></part> </message> <!-- portType: 用來定義服務器端的SEI operation : 用來指定SEI中的處理請求的方法 input : 指定客戶端應用傳過來的數據, 會引用上面的定義的<message> output : 指定服務器端返回給客戶端的數據, 會引用上面的定義的<message> --> <portType name="HelloServiceImpl"> <operation name="sayHello"> <input wsam:Action="http://impl.service.cr.com/HelloServiceImpl/sayHelloRequest" message="tns:sayHello"></input> <output wsam:Action="http://impl.service.cr.com/HelloServiceImpl/sayHelloResponse" message="tns:sayHelloResponse"></output> </operation> </portType> <!-- binding : 用於定義SEI的實現類 type屬性: 引用上面的<portType> <soap:binding style="document"> : 綁定的數據是一個document(xml) operation : 用來定義實現的方法 <soap:operation style="document" /> 傳輸的是document(xml) input: 指定客戶端應用傳過來的數據 <soap:body use="literal" /> : 文本數據 output : 指定服務器端返回給客戶端的數據 <soap:body use="literal" /> : 文本數據 --> <binding name="HelloServiceImplPortBinding" type="tns:HelloServiceImpl"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding> <operation name="sayHello"> <soap:operation soapAction=""></soap:operation> <input> <soap:body use="literal"></soap:body> </input> <output> <soap:body use="literal"></soap:body> </output> </operation> </binding> <!-- service : 一個webservice的容器 name屬性: 它用一指定客戶端容器類 port : 用來指定一個服務器端處理請求的入口(就SEI的實現) binding屬性: 引用上面定義的<binding> address : 當前webservice的請求地址 --> <service name="HelloServiceImplService"> <port name="HelloServiceImplPort" binding="tns:HelloServiceImplPortBinding"> <soap:address location="http://localhost:8082/webserviceserver/helloService"></soap:address> </port> </service> </definitions>
同時可以參考地址:https://www.cnblogs.com/yangh965/p/5046841.html
圖解:
5、Web Service-整合CXF