原文: https://howtodoinjava.com/spring-boot/spring-soap-client-webservicetemplate/

学习使用 Spring Boot Soap 客户端使用 SOAP Web 服务以及使用 JAXB maven 插件自动生成客户端代理类。 创建 SOAP Web 服务不在本教程的讨论范围内,但是您可以在中学习它。

阅读更多:在 Spring Framework 中创建 SOAP WS

先决条件

在运行此示例之前,我们需要准备好一个 SOAP 服务,该服务将从该客户端代码中调用。 为此,您可以下载附件的 maven 项目(在文章结尾),然后在本地工作区中运行它并使用它。

运行此 SOAP 服务器项目后,您将从http://localhost:8080/service/studentDetailsWsdl.wsdl获取 WSDL。 将 WSDL 下载为studentDetailsWsdl.wsdl,稍后将其放置在客户端项目的resources/wsdl文件夹中,该文件夹将在下一步创建以生成客户端代理代码。

Spring Boot Soap 客户端的技术栈

  • JDK 1.8,Eclipse,Maven – 开发环境
  • SpringBoot – 基础应用程序框架
  • maven-jaxb2-plugin插件 – 用于生成 JAXB 存根
  • SpringBoot CommandLineRunner – 测试客户端代码

项目结构

为该演示创建的类和文件如下所示。

Spring Boot SOAP 客户端 – `WebServiceTemplate`示例 - 图1

SOAP 客户端项目结构

使用WebServiceTemplate创建 Spring 客户端

创建启动项目

仅从具有Web Services依赖关系的 SPRING 初始化器站点创建一个 spring boot 项目。 选择依赖项并提供适当的 Maven GAV 坐标后,以压缩格式下载项目。 解压缩,然后将 eclipse 中的项目导入为 maven 项目。

Spring Boot SOAP 客户端 – `WebServiceTemplate`示例 - 图2

Spring boot 项目生成

生成 SOAP 域类

现在使用maven-jaxb2-plugin maven 插件生成 JAXB 注解的存根类。 为此,将此 maven 插件添加到项目的pom.xml中。

pom.xml

  1. <plugin>
  2. <groupId>org.jvnet.jaxb2.maven2</groupId>
  3. <artifactId>maven-jaxb2-plugin</artifactId>
  4. <version>0.13.2</version>
  5. <executions>
  6. <execution>
  7. <goals>
  8. <goal>generate</goal>
  9. </goals>
  10. </execution>
  11. </executions>
  12. <configuration>
  13. <generatePackage>com.example.howtodoinjava.schemas.school</generatePackage>
  14. <generateDirectory>${project.basedir}/src/main/java</generateDirectory>
  15. <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
  16. <schemaIncludes>
  17. <include>*.wsdl</include>
  18. </schemaIncludes>
  19. </configuration>
  20. </plugin>

此插件将在项目的src目录的com.example.howtodoinjava.springbootsoapclient包中生成类,并且此插件将检查类的生成时间戳,以便仅在WSDL中发生任何更改时才生成这些类。

使用WebServiceTemplate创建 SOAP 客户端

创建一个名为SOAPConnector.java的类,该类将充当对 Web 服务的所有请求的通用 Web 服务客户端。

SOAPConnector.java

  1. package com.example.howtodoinjava.springbootsoapclient;
  2. import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
  3. public class SOAPConnector extends WebServiceGatewaySupport {
  4. public Object callWebService(String url, Object request){
  5. return getWebServiceTemplate().marshalSendAndReceive(url, request);
  6. }
  7. }
  1. SOAPConnector类是对WebServiceGatewaySupport的扩展,它基本上是通过getWebServiceTemplate()方法提供的WebServiceTemplate内部实现注入一个接口。
  2. 我们将使用此WebServiceTemplate来调用 SOAP 服务。
  3. 该类还期望注入一个名为MarshallerUnmarshaller的 spring bean,它们将由配置类提供,我们将在下面看到。

Spring bean 配置

现在,我们需要创建一个用@Configuration注解的配置类,该类将具有SOAPConnector所需的必需的 bean 定义,以使其正常工作。

Config.java

  1. package com.example.howtodoinjava.springbootsoapclient;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.oxm.jaxb.Jaxb2Marshaller;
  5. @Configuration
  6. public class Config {
  7. @Bean
  8. public Jaxb2Marshaller marshaller() {
  9. Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
  10. // this is the package name specified in the <generatePackage> specified in
  11. // pom.xml
  12. marshaller.setContextPath("com.example.howtodoinjava.schemas.school");
  13. return marshaller;
  14. }
  15. @Bean
  16. public SOAPConnector soapConnector(Jaxb2Marshaller marshaller) {
  17. SOAPConnector client = new SOAPConnector();
  18. client.setDefaultUri("http://localhost:8080/service/student-details");
  19. client.setMarshaller(marshaller);
  20. client.setUnmarshaller(marshaller);
  21. return client;
  22. }
  23. }
  1. WebServiceGatewaySupport需要MarshallerUnmarshaller,它们是Jaxb2Marshaller类的实例。
  2. 它使用com.example.howtodoinjava.schemas.school作为 JAXB 类的基本包。 它将使用此包创建 JAXB 上下文。
  3. 我们将使用此Jaxb2Marshaller bean 作为SOAPConnector bean 的Marshaller/Unmarshaller

使用CommandLineRunner测试

为简单起见,我们将创建一个 Spring Boot 命令行运行程序,该加载程序将加载 spring 上下文并调用处理器方法,并将命令行参数传递给该方法。 实时地,我们需要用一些其他代码替换此命令行运行程序,这些代码将更适合企业。

我们需要在SpringBootApplication类中添加此命令行运行器 bean,如下。

SpringBootSoapClientApplication.java

  1. package com.example.howtodoinjava.springbootsoapclient;
  2. import org.springframework.boot.CommandLineRunner;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.annotation.Bean;
  6. import com.example.howtodoinjava.schemas.school.StudentDetailsRequest;
  7. import com.example.howtodoinjava.schemas.school.StudentDetailsResponse;
  8. @SpringBootApplication
  9. public class SpringBootSoapClientApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(SpringBootSoapClientApplication.class, args);
  12. }
  13. @Bean
  14. CommandLineRunner lookup(SOAPConnector soapConnector) {
  15. return args -> {
  16. String name = "Sajal";//Default Name
  17. if(args.length>0){
  18. name = args[0];
  19. }
  20. StudentDetailsRequest request = new StudentDetailsRequest();
  21. request.setName(name);
  22. StudentDetailsResponse response =(StudentDetailsResponse) soapConnector.callWebService("http://localhost:8080/service/student-details", request);
  23. System.out.println("Got Response As below ========= : ");
  24. System.out.println("Name : "+response.getStudent().getName());
  25. System.out.println("Standard : "+response.getStudent().getStandard());
  26. System.out.println("Address : "+response.getStudent().getAddress());
  27. };
  28. }
  29. }

在这里,我们从命令行获取搜索参数,并创建StudentDetailsRequest对象,并使用SOAPConnector调用 SOAP Web 服务。

一些可选配置

打开application.properties并添加以下配置

application.properties

  1. server.port = 9090
  2. logging.level.org.springframework.ws=TRACE

在这里,我们用server.port = 9090将默认端口覆盖为9090,因为您已经注意到我们的示例 SOAP 服务在默认端口8080中运行,因为两个 Java 进程不能在同一端口中运行。

另外,我们正在通过logging.level.org.springframework.ws=TRACEorg.springframework.ws软件包启用TRACE日志记录。 这将在控制台中打印 SOAP 负载。

这就是我们使用 Spring Boot 消费 SOAP 服务所需要做的一切,现在是时候进行测试了。

示例

现在使用 maven 命令mvn clean install来构建应用程序。 我们可以从命令提示符下通过命令java -jar target\spring-boot-soap-client-0.0.1-SNAPSHOT.jar Lokesh调用命令行运行程序。

请注意,我们在此处传递了一个命令行参数Lokesh,该参数将在CommandLineRunner bean 的查找方法中使用。 如果没有传递任何名称,我们将在该方法中传递一个默认名称。

调用命令行运行程序后,我们应该看到 SOAP 服务输出,并且响应已正确解组到 JAXB 对象StudentDetailsResponse。 同样,我们可以在 TRACE 日志中看到完整的 SOAP 请求/响应,如下所示。

输出

  1. 2017-10-09 23:20:45.548 TRACE 9204 --- [ main] o.s.ws.client.MessageTracing.received : Received response [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:StudentDetailsResponse xmlns:ns2="https://www.howtodoinjava.com/xml/school"><ns2:Student><ns2:name>Sajal</ns2:name><ns2:standard>5</ns2:standard><ns2:address>Pune</ns2:address></ns2:Student></ns2:StudentDetailsResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>] for request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:StudentDetailsRequest xmlns:ns2="https://www.howtodoinjava.com/xml/school"><ns2:name>Sajal</ns2:name></ns2:StudentDetailsRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>]
  2. Got Response As below ========= :
  3. Name : Lokesh
  4. Standard : 6
  5. Address : Delhi

总结

在本 SOAP 教程中,我们学习了如何轻松地从 Spring Boot Soap 客户端中使用来使用 SOAP 服务。 每当需要使用任何此类 SOAP 服务时,都可以使用此方法。 希望这对您有用。

请在评论部分添加您的反馈。

下载源码

学习愉快!