0
点赞
收藏
分享

微信扫一扫

Spring Boot 消费 SOAP 服务

MaxWen 2021-09-21 阅读 41

本文介绍 Spring Boot 2 消费 SOAP 服务的方法。

本文示例基于 Spring Boot 开发 SOAP 服务。


目录

  • 开发环境
  • 基础示例
  • 总结

开发环境

  • Oracle JDK 1.8.0_201
  • Apache Maven 3.6.0
  • IntelliJ IDEA (Version 2018.3.3)

基础示例

  1. 创建 Spring Boot 工程,参考:IntelliJ IDEA 创建 Spring Boot 工程。

  2. 在 pom 文件中添加 spring-boot-starter-web-services 依赖。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
  1. pom 文件中添加 maven-jaxb2-plugin 插件。
<plugin>
  <groupId>org.jvnet.jaxb2.maven2</groupId>
  <artifactId>maven-jaxb2-plugin</artifactId>
  <version>0.14.0</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <schemaLanguage>WSDL</schemaLanguage>
    <generateDirectory>src/main/java</generateDirectory>
    <generatePackage>tutorial.spring.boot.soap.consumer.generated</generatePackage>
    <schemas>
      <schema>
        <url>http://127.0.0.1:8080/ws/user.wsdl</url>
      </schema>
    </schemas>
  </configuration>
</plugin>

maven-jaxb2-plugin 提供了一种根据 WSDL 中 <xs:schema> 描述生成 Java 类的方法,配置说明:

  • generateDirectory:生成 Java 类文件的根目录
  • generatePackage:生成 Java 类文件的包路径
  • schemas:WSDL URL

完整的 pom 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>tutorial.spring.boot</groupId>
    <artifactId>spring-boot-soap-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-soap-consumer</name>
    <description>Demo project for Spring Boot SOAP consumer</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.14.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaLanguage>WSDL</schemaLanguage>
                    <generateDirectory>src/main/java</generateDirectory>
                    <generatePackage>tutorial.spring.boot.soap.consumer.generated</generatePackage>
                    <schemas>
                        <schema>
                            <url>http://127.0.0.1:8080/ws/user.wsdl</url>
                        </schema>
                    </schemas>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
  1. 执行 mvn compile 生成 Java 类文件,生成文件:
|-- src
     |-- main
          |-- java
              |-- META-INF
                  |-- sun-jaxb.episode
              |-- tutorial.spring.boot.soap.consumer
                  |-- generated
                      |-- Gender.java
                      |-- ObjectFactory.java
                      |-- package-info.java
                      |-- User.java
                      |-- UserRequest.java
                      |-- UserResponse.java

注意:编译过程出现以下警告错误,但不影响最终结果。

[WARNING] The URI [http://127.0.0.1:8080/ws/user.wsdl] seems to represent an absolute HTTP or HTTPS URL. Getting the last modification timestamp is only possible if the URL is accessible and if the server returns the [Last-Modified] header correctly. This method is not reliable and is likely to fail. In this case the last modification timestamp will be assumed to be unknown.
[ERROR] Could not retrieve the last modification timestamp for the URI [http://127.0.0.1:8080/ws/user.wsdl] from the HTTP URL connection. The [Last-Modified] header was probably not set correctly.
[WARNING] Last modification of the URI [http://127.0.0.1:8080/ws/user.wsdl] is not known.
  1. 创建访问 Web Service 的 Client 类。
package tutorial.spring.boot.soap.consumer.client;

import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.client.core.SoapActionCallback;
import tutorial.spring.boot.soap.consumer.generated.UserRequest;
import tutorial.spring.boot.soap.consumer.generated.UserResponse;

public class UserClient extends WebServiceGatewaySupport {

    public UserResponse getUser(String name) {
        UserRequest request = new UserRequest();
        request.setName(name);
        return (UserResponse) getWebServiceTemplate()
                .marshalSendAndReceive("http://127.0.0.1:8080/ws/user", request,
                        new SoapActionCallback("http://tutorial.spring.boot/soap/produce/user/EmployeeDetailRequest"));
    }
}

访问 Web Service 的 Client 类继承自 WebServiceGatewaySupport,使用基类提供的 WebServiceTemplate 进行实际的 SOAP 交换。

  1. 创建 Web Service 配置类。
package tutorial.spring.boot.soap.consumer.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import tutorial.spring.boot.soap.consumer.client.UserClient;

@Configuration
public class WebServiceConfig {

    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("tutorial.spring.boot.soap.consumer.generated");
        return marshaller;
    }

    @Bean
    public UserClient userClient(Jaxb2Marshaller marshaller) {
        UserClient client = new UserClient();
        client.setDefaultUri("http://127.0.0.1:8080/ws");
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }
}

Spring WS 使用了 Spring Framework 的 OXM 模块,OXM 模块中 Jaxb2Marshaller 用于序列化和反序列化 XML 请求。marshaller 指向生成域对象的集合,并使用它们在 XML 和 POJO 间进行序列化和反序列化。

  1. 创建调用 Web Service 的业务类(模拟实际业务逻辑,用于后续测试)。
package tutorial.spring.boot.soap.consumer.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tutorial.spring.boot.soap.consumer.client.UserClient;
import tutorial.spring.boot.soap.consumer.generated.UserResponse;

@RestController
@RequestMapping("/user")
public class UserController {

    private final UserClient userClient;

    public UserController(UserClient userClient) {
        this.userClient = userClient;
    }

    @GetMapping
    public String get(@RequestParam("name") String name) throws JsonProcessingException {
        UserResponse response = userClient.getUser(name);
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(response);
    }
}
  1. 启动应用,浏览器输入一下 3 个地址执行测试。
    http://localhost:8081/user?name=Mike
    http://localhost:8081/user?name=Ketty
    http://localhost:8081/user?name=Tom

总结

  1. 创建 Spring Boot 应用,添加 spring-boot-starter-web-servicesmaven-jaxb2-plugin 插件;
  2. 执行 mvn compile 基于 WSDL 生成域对象;
  3. 创建访问 Web Service 的 Client 类;
  4. 创建 Web Service 配置类;
  5. 创建调用 Web Service 的业务类;
  6. 启动应用,执行测试。
举报

相关推荐

0 条评论