Spring Boot 2.7.5中如何将SOAP默认文档风格改为RPC风格?
Spring Boot 2.7.5 配置SOAP服务为RPC风格
1. 确认依赖
确保项目引入Spring Web Services的starter依赖,根据构建工具选择对应配置:
Maven
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency>
Gradle
implementation 'org.springframework.boot:spring-boot-starter-web-services'
2. 配置RPC核心组件
创建SOAP配置类,替换默认的Document风格配置,指定RPC相关Bean:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.ws.server.endpoint.mapping.SimpleRpcEndpointMapping; import org.springframework.ws.soap.server.endpoint.DefaultMethodEndpointAdapter; import org.springframework.ws.soap.server.endpoint.adapter.method.RpcMethodArgumentResolver; import org.springframework.ws.soap.server.endpoint.adapter.method.RpcMethodReturnValueHandler; import org.springframework.ws.soap.saaj.SaajSoapMessageFactory; import java.util.List; @Configuration public class SoapRpcConfiguration { // 配置RPC风格的消息工厂 @Bean public SaajSoapMessageFactory messageFactory() { SaajSoapMessageFactory factory = new SaajSoapMessageFactory(); factory.setSoapVersion(org.springframework.ws.soap.SoapVersion.SOAP_11); return factory; } // 配置RPC端点映射,替换为你的服务命名空间 @Bean public SimpleRpcEndpointMapping rpcEndpointMapping() { SimpleRpcEndpointMapping mapping = new SimpleRpcEndpointMapping(); mapping.setDefaultNamespaceUri("http://your-service-namespace.com"); return mapping; } // 配置端点适配器,支持RPC方法的参数解析与返回值处理 @Bean public DefaultMethodEndpointAdapter defaultMethodEndpointAdapter() { DefaultMethodEndpointAdapter adapter = new DefaultMethodEndpointAdapter(); adapter.setMethodArgumentResolvers(List.of(new RpcMethodArgumentResolver())); adapter.setMethodReturnValueHandlers(List.of(new RpcMethodReturnValueHandler())); return adapter; } }
3. 编写RPC风格端点
创建端点类,使用@Endpoint注解标记,方法通过@RpcMethod指定SOAP操作名称:
import org.springframework.ws.server.endpoint.annotation.Endpoint; import org.springframework.ws.server.endpoint.annotation.RpcMethod; import org.springframework.ws.server.endpoint.annotation.RequestPayload; import org.springframework.ws.server.endpoint.annotation.ResponsePayload; @Endpoint public class SampleRpcEndpoint { // 对应SOAP操作"GetUserRequest"的RPC方法 @RpcMethod(operationName = "GetUserRequest") @ResponsePayload public GetUserResponse getUser(@RequestPayload GetUserRequest request) { GetUserResponse response = new GetUserResponse(); // 填充业务逻辑返回结果 response.setUserId(request.getUserId()); response.setUserName("John Doe"); response.setEmail("john.doe@example.com"); return response; } }
4. 生成RPC风格WSDL(可选)
如果需要自动生成RPC风格WSDL,配置DefaultWsdl11Definition时指定style为RPC:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition; import org.springframework.xml.xsd.SimpleXsdSchema; import org.springframework.xml.xsd.XsdSchema; import org.springframework.core.io.ClassPathResource; @Configuration public class WsdlConfiguration { @Bean public XsdSchema userSchema() { return new SimpleXsdSchema(new ClassPathResource("user-service.xsd")); } @Bean(name = "userservice") public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema userSchema) { DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition(); wsdl11Definition.setPortTypeName("UserServicePort"); wsdl11Definition.setLocationUri("/ws"); wsdl11Definition.setTargetNamespace("http://your-service-namespace.com"); wsdl11Definition.setSchema(userSchema); // 设置WSDL风格为RPC wsdl11Definition.setStyle(org.springframework.ws.wsdl.wsdl11.WsdlDefinition.Style.RPC); return wsdl11Definition; } }
注意:对应的XSD文件需符合RPC风格定义,使用element标记请求、响应结构,而非Document风格的嵌套complexType。
5. 验证配置
启动应用后,访问WSDL地址(如http://localhost:8080/ws/userservice.wsdl),查看binding节点是否包含style="rpc",以此确认RPC配置生效。
内容的提问来源于stack exchange,提问作者RANDRIAMILASOA Stanislas
相关产品推荐
相关产品推荐

