Spring Boot整合spring-ws调用web service服务

前言

前面我们已经整合spring-ws实现了web service的服务端:Spring Boot整合spring-ws开发web service

接下来就是实现客户端进行调用了。

添加依赖

客户端,同样的需要先添加依赖:


  1. org.springframework.boot
  2. spring-boot-starter-ws
  3. wsdl4j
  4. wsdl4j

获取wsdl文件

服务端由一个xsd文件开始,客户端则是由一个wsdl文件开始。

获取wsdl文件也十分简单,用浏览器访问web service地址,然后另存为即可。当然也可以直接用url地址来生成代码,只不过我习惯本地另存为后再生成。

完整的wsdl文件内容如下:


  1. xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
  2. xmlns:sch="http://www.dexcoder.com/ws"
  3. xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  4. xmlns:tns="http://www.dexcoder.com/ws" targetNamespace="http://www.dexcoder.com/ws">
  5. xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://www.dexcoder.com/ws">
  6. name="getCountryRequest">
  7. name="name" type="xs:string"/>
  8. name="getCountryResponse">
  9. name="country" type="tns:country"/>
  10. name="country">
  11. name="name" type="xs:string"/>
  12. name="population" type="xs:int"/>
  13. name="capital" type="xs:string"/>
  14. name="currency" type="tns:currency"/>
  15. name="currency">
  16. base="xs:string">
  17. value="GBP"/>
  18. value="EUR"/>
  19. value="PLN"/>
  20. name="getCountryResponse">
  21. element="tns:getCountryResponse" name="getCountryResponse">
  22. name="getCountryRequest">
  23. element="tns:getCountryRequest" name="getCountryRequest">
  24. name="CountriesPort">
  25. name="getCountry">
  26. message="tns:getCountryRequest" name="getCountryRequest">
  27. message="tns:getCountryResponse" name="getCountryResponse">
  28. name="CountriesPortSoap11" type="tns:CountriesPort">
  29. style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
  30. name="getCountry">
  31. soapAction=""/>
  32. name="getCountryRequest">
  33. use="literal"/>
  34. name="getCountryResponse">
  35. use="literal"/>
  36. name="CountriesPortService">
  37. binding="tns:CountriesPortSoap11" name="CountriesPortSoap11">

添加maven的jaxb2插件生成代码

跟服务端根据xsd来生成代码类似,客户端同样可以根据wsdl来生成代码。maven插件依赖:


  1. org.jvnet.jaxb2.maven2
  2. maven-jaxb2-plugin
  3. 0.12.3
  4. generate
  5. WSDL
  6. com.dexcoder.ws
  7. ${basedir}/src/main/java
  8. ${basedir}/src/main/resources/schemas
  9. *.wsdl

同样mvn install之后将生成客户端代码。这里生成的代码跟我们前面发布的服务端代码应该是一样的,当然包名可能不同这个由你指定。

在生成代码的同时会生成META-INF文件夹,这个可以移到resources目录下或者直接删除都没有关系。生成后的项目结构图:

编写ws客户端

生成了代码之后,编写客户端变的很容易,具体代码如下:


  1. public class WsClient extends WebServiceGatewaySupport {
  2. public GetCountryResponse getCountry(String name) {
  3. GetCountryRequest request = new GetCountryRequest();
  4. request.setName(name);
  5. GetCountryResponse response = (GetCountryResponse) getWebServiceTemplate().marshalSendAndReceive(
  6. "http://localhost:8080/ws/countries.wsdl", request);
  7. return response;
  8. }
  9. }

配置ws客户端

编写完一切代码之后,同样需要配置到spring boot才行,ContextPath指定刚才生成代码所在的包名,它会到该包下去寻找相应的类自动进行数据转换:


  1. @Configuration
  2. public class WSConfig {
  3. @Bean
  4. public Jaxb2Marshaller marshaller() {
  5. Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
  6. marshaller.setContextPath("com.dexcoder.ws");
  7. return marshaller;
  8. }
  9. @Bean
  10. public WsClient wsClient(Jaxb2Marshaller marshaller) {
  11. WsClient client = new WsClient();
  12. client.setDefaultUri("http://localhost:8080/ws/countries.wsdl");
  13. client.setMarshaller(marshaller);
  14. client.setUnmarshaller(marshaller);
  15. return client;
  16. }
  17. }

运行

到这里所有的调用代码都已经完成了,剩下的就是运行来检测是否调用成功。

这里我们来编写一个Controller来简单测试一下。


  1. @RestController
  2. public class IndexController {
  3. @Autowired
  4. private WsClient wsClient;
  5. @RequestMapping("callws")
  6. public Object callWs() {
  7. GetCountryResponse response = wsClient.getCountry("hello");
  8. return response.getCountry();
  9. }
  10. }

使用了RestController,直接将调用ws返回的数据用json格式输出到页面。

启动spring boot,访问http://localhost:8081/callws 这里把端口换成了8081,因为默认的8080已经被前面的服务端占用了。

可以看到成功调用了ws的服务端并返回了数据,hello部分为我们发送过去的参数:

标签: 
  • spring-ws 
  • web service 
  • Spring boot

你可能感兴趣的:(spring)