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
图解:
原文地址:https://www.cnblogs.com/Mrchengs/p/10569046.html
时间: 2024-10-07 17:55:05