本文介绍 Spring Boot 2 消费 SOAP 服务的方法。
本文示例基于 Spring Boot 开发 SOAP 服务。
目录
- 开发环境
- 基础示例
- 总结
开发环境
- Oracle JDK 1.8.0_201
- Apache Maven 3.6.0
- IntelliJ IDEA (Version 2018.3.3)
基础示例
创建 Spring Boot 工程,参考:IntelliJ IDEA 创建 Spring Boot 工程。
在 pom 文件中添加
spring-boot-starter-web-services
依赖。
org.springframework.boot
spring-boot-starter-web-services
- 在
pom
文件中添加maven-jaxb2-plugin
插件。
org.jvnet.jaxb2.maven2
maven-jaxb2-plugin
0.14.0
generate
WSDL
src/main/java
tutorial.spring.boot.soap.consumer.generated
http://127.0.0.1:8080/ws/user.wsdl
maven-jaxb2-plugin
提供了一种根据 WSDL 中
描述生成 Java 类的方法,配置说明:
-
generateDirectory
:生成 Java 类文件的根目录 -
generatePackage
:生成 Java 类文件的包路径 -
schemas
:WSDL URL
完整的 pom
文件如下:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
tutorial.spring.boot
spring-boot-soap-consumer
0.0.1-SNAPSHOT
spring-boot-soap-consumer
Demo project for Spring Boot SOAP consumer
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web-services
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-maven-plugin
org.jvnet.jaxb2.maven2
maven-jaxb2-plugin
0.14.0
generate
WSDL
src/main/java
tutorial.spring.boot.soap.consumer.generated
http://127.0.0.1:8080/ws/user.wsdl
- 执行
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.
- 创建访问 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 交换。
- 创建 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 间进行序列化和反序列化。
- 创建调用 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);
}
}
- 启动应用,浏览器输入一下 3 个地址执行测试。
http://localhost:8081/user?name=Mike
http://localhost:8081/user?name=Ketty
http://localhost:8081/user?name=Tom
总结
- 创建 Spring Boot 应用,添加
spring-boot-starter-web-services
和maven-jaxb2-plugin
插件; - 执行
mvn compile
基于 WSDL 生成域对象; - 创建访问 Web Service 的 Client 类;
- 创建 Web Service 配置类;
- 创建调用 Web Service 的业务类;
- 启动应用,执行测试。