
本文将指导你如何在 Spring Boot 应用中接收 SOAP 请求,将其转换为 JSON 格式,并调用 REST API。同时,还将介绍如何接收 REST API 的 JSON 响应,并将其转换回 SOAP 响应。通过 Spring Web Services 和 Spring MVC 的强大功能,可以简化 SOAP 和 REST 通信之间的桥接过程,无需手动进行复杂的转换。
搭建 Spring Boot 项目
首先,你需要创建一个 Spring Boot 项目。可以使用 Spring Initializr (https://www.php.cn/link/4ac20f72e05b86b3dc759608b60f5d67) 来快速生成项目骨架。 在 Spring Initializr 中,选择以下依赖:
- Spring Web Services
- Spring Web
- JAXB (如果你的 SOAP 定义使用了 JAXB)
定义 SOAP 端点
使用 @Endpoint 注解创建一个类,用于处理 SOAP 请求。@PayloadRoot 注解用于指定处理特定 SOAP 消息的方法。
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
import org.springframework.web.client.RestTemplate;
@Endpoint
public class YourEndpoint {
private final RestTemplate restTemplate;
private static final String REST_URL = "your_rest_api_url"; // Replace with your REST API URL
public YourEndpoint(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@PayloadRoot(localPart = "YourRequestFromWsdlRequest", namespace = "your_wsdl_namespace")
@ResponsePayload
public YourResponseFromWsdl method(@RequestPayload YourRequestFromWsdl request) {
// Call REST API with the request
YourResponseFromWsdl response = restTemplate.postForObject(REST_URL, request, YourResponseFromWsdl.class);
return response;
}
}代码解释:
- @Endpoint: 声明该类为一个 SOAP 端点。
- @PayloadRoot: 指定处理的 SOAP 消息的本地名称和命名空间。 localPart 对应于 WSDL 中定义的请求消息的名称,namespace 对应于 WSDL 中定义的命名空间。 请确保将 your_wsdl_namespace 替换为 WSDL 中定义的实际命名空间。
- @RequestPayload: 将 SOAP 请求的消息体绑定到 YourRequestFromWsdl 对象。
- @ResponsePayload: 将方法的返回值作为 SOAP 响应的消息体返回。
- RestTemplate: 用于调用 REST API。你需要注入一个 RestTemplate 实例。
- REST_URL: 替换为你的 REST API 的 URL。
依赖注入 RestTemplate:
你需要配置 RestTemplate bean。 在你的 Spring Boot 应用配置类中添加以下代码:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}定义 JAXB 对象
你需要根据你的 WSDL 文件生成对应的 Java 对象。 可以使用 xjc 工具来完成这个任务。 例如:
xjc your_wsdl_file.wsdl -d src/main/java
这将生成与 WSDL 文件对应的 Java 类,包括 YourRequestFromWsdl 和 YourResponseFromWsdl。
确保命名空间正确:
在生成的 JAXB 对象中,确保 @XmlType 和 @XmlRootElement 注解的 namespace 属性与你的 WSDL 文件中的命名空间一致。
使用 WebClient (可选)
除了 RestTemplate,你还可以使用 WebClient 来调用 REST API。 WebClient 是一个非阻塞的、响应式的 HTTP 客户端,可以提供更好的性能和可伸缩性。
import org.springframework.web.reactive.function.client.WebClient;
@Endpoint
public class YourEndpoint {
private final WebClient webClient;
private static final String REST_URL = "your_rest_api_url";
public YourEndpoint(WebClient webClient) {
this.webClient = webClient;
}
@PayloadRoot(localPart = "YourRequestFromWsdlRequest", namespace = "your_wsdl_namespace")
@ResponsePayload
public YourResponseFromWsdl method(@RequestPayload YourRequestFromWsdl request) {
YourResponseFromWsdl response = webClient.post()
.uri(REST_URL)
.bodyValue(request)
.retrieve()
.bodyToMono(YourResponseFromWsdl.class)
.block(); // Use block() for synchronous execution. Consider asynchronous handling in production.
return response;
}
}配置 WebClient:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class AppConfig {
@Bean
public WebClient webClient() {
return WebClient.create();
}
}配置 Spring Web Services
在你的 Spring Boot 应用中,你需要配置 Spring Web Services 来处理 SOAP 请求。 创建一个配置类,并添加以下代码:
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;
@EnableWs
@Configuration
public class WebServiceConfig {
@Bean
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean<>(servlet, "/ws/*"); // Map to /ws/*
}
@Bean(name = "yourWsdlName") // Replace with your WSDL name
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema yourSchema) {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("YourPort"); // Replace with your PortType name
wsdl11Definition.setLocationUri("/ws");
wsdl11Definition.setTargetNamespace("your_wsdl_namespace"); // Replace with your WSDL namespace
wsdl11Definition.setSchema(yourSchema);
return wsdl11Definition;
}
@Bean
public XsdSchema yourSchema() {
return new SimpleXsdSchema(new ClassPathResource("your_schema.xsd")); // Replace with your schema file
}
} 代码解释:
- @EnableWs: 启用 Spring Web Services。
- MessageDispatcherServlet: Spring Web Services 的核心 Servlet,用于处理 SOAP 请求。
- DefaultWsdl11Definition: 用于生成 WSDL 文件。
- XsdSchema: 用于定义 WSDL 文件中使用的 XML Schema。
配置要点:
- 将 yourWsdlName 替换为你的 WSDL 文件的名称。
- 将 YourPort 替换为你的 WSDL 文件中定义的 portType 的名称。
- 将 your_wsdl_namespace 替换为你的 WSDL 文件中定义的命名空间。
- 将 your_schema.xsd 替换为你的 XML Schema 文件的路径。 你需要根据你的 WSDL 文件创建一个 XSD 文件。
异常处理
在实际应用中,需要处理可能发生的异常,例如 REST API 调用失败或数据转换错误。 可以使用 @ExceptionHandler 注解来处理这些异常。
注意事项
- 确保你的 WSDL 文件是有效的,并且生成的 Java 对象与 WSDL 文件中的定义一致。
- 在生产环境中,应该使用异步方式处理 REST API 调用,以避免阻塞 SOAP 请求的处理线程。
- 考虑添加日志记录和监控,以便更好地了解你的服务的运行状况。
- 确保你的 Spring Boot 应用已正确配置,并且所有必要的依赖项都已添加到项目中。
- 仔细检查命名空间和本地名称,确保它们与 WSDL 文件中的定义完全匹配。
- 对于 WebClient,在生产环境中使用 block() 可能会导致性能问题。 考虑使用异步方式处理响应。
通过以上步骤,你就可以在 Spring Boot 应用中成功地接收 SOAP 消息,将其转换为 JSON 格式,并调用 REST API。 Spring Web Services 和 Spring MVC 提供了强大的功能,可以简化 SOAP 和 REST 通信之间的桥接过程。










