如何创建接收SOAP XML的REST Endpoint并调用Spring Boot SOAP Endpoint返回响应
实现REST端点转发SOAP请求到Spring Boot SOAP服务
1. 添加项目依赖
如果使用Maven,在pom.xml中引入以下依赖:
<dependencies> <!-- Spring Web 用于搭建REST端点 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Web Services 用于SOAP客户端调用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <!-- XML处理依赖 --> <dependency> <groupId>com.sun.xml.messaging.saaj</groupId> <artifactId>saaj-impl</artifactId> <version>1.5.3</version> </dependency> </dependencies>
2. 配置SOAP客户端
创建配置类初始化WebServiceTemplate,指定目标SOAP服务的地址:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.oxm.jaxb.Jaxb2Marshaller; import org.springframework.ws.client.core.WebServiceTemplate; @Configuration public class SoapClientConfig { @Bean public Jaxb2Marshaller marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); // 替换为你的SOAP服务生成的JAXB类所在包路径 marshaller.setContextPath("com.yourcompany.soap.generated"); return marshaller; } @Bean public WebServiceTemplate webServiceTemplate(Jaxb2Marshaller marshaller) { WebServiceTemplate template = new WebServiceTemplate(); template.setMarshaller(marshaller); template.setUnmarshaller(marshaller); // 设置目标SOAP端点的完整URL template.setDefaultUri("http://localhost:8080/ws/your-soap-service"); return template; } }
提示:如果不需要对象绑定,仅需转发XML字符串,可以省略JAXB配置,直接使用字符串源进行请求发送。
3. 编写REST转发控制器
创建REST控制器,接收SOAP XML请求,转发到目标SOAP服务并返回响应:
import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.ws.client.core.WebServiceTemplate; import org.springframework.ws.soap.client.core.SoapActionCallback; import javax.xml.transform.Source; import javax.xml.transform.StringSource; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import java.io.StringWriter; @RestController public class SoapForwardController { private final WebServiceTemplate webServiceTemplate; public SoapForwardController(WebServiceTemplate webServiceTemplate) { this.webServiceTemplate = webServiceTemplate; } @PostMapping(value = "/forward-soap", consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE) public String forwardSoapRequest(@RequestBody String soapXml) throws Exception { // 根据目标SOAP服务要求设置SOAP Action,无需则可省略此参数 SoapActionCallback soapAction = new SoapActionCallback("http://yourcompany.com/soap/action/YourOperation"); Source requestSource = new StringSource(soapXml); StringWriter responseWriter = new StringWriter(); // 发送请求并将响应转换为字符串 webServiceTemplate.sendSourceAndReceiveToResult( requestSource, soapAction, new StreamResult(responseWriter) ); return responseWriter.toString(); } }
4. 测试端点
使用curl命令测试REST转发端点:
curl -X POST http://localhost:8080/forward-soap \ -H "Content-Type: application/xml" \ -d '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:demo="http://yourcompany.com/soap/demo"> <soapenv:Header/> <soapenv:Body> <demo:HelloRequest> <demo:name>Test</demo:name> </demo:HelloRequest> </soapenv:Body> </soapenv:Envelope>'
内容的提问来源于stack exchange,提问作者Maha Sonu
相关产品推荐
相关产品推荐

