使用Spring WS发布SOAP端点 如何实现类似Apache CXF的EndpointImpl功能
Spring-WS 实现多WSDL SOAP服务发布操作指南
Spring-WS 没有直接等价于 Apache CXF EndpointImpl 的开箱即用类,但可以通过 DefaultWsdl11Definition + 注解式端点的组合,实现和 EndpointImpl 完全一致的「绑定WSDL文件、指定服务访问URL、注册业务处理逻辑」能力,多WSDL场景的具体实现步骤如下:
1 引入基础依赖
首先在项目pom.xml中引入Spring-WS核心依赖:
<dependency> <groupId>org.springframework.ws</groupId> <artifactId>spring-ws-core</artifactId> <version>3.1.4</version> <!-- 可替换为对应Spring版本适配的最新版 --> </dependency>
2 单WSDL服务发布流程
每个WSDL对应一套独立的服务配置,逻辑和CXF初始化单个EndpointImpl实例完全对应:
- 将所有WSDL文件统一放置到项目资源目录,比如
src/main/resources/wsdl/下 - 注册WSDL绑定Bean,指定服务访问路径:
@Configuration public class SoapServiceConfig { // 第一个WSDL对应的服务,WSDL访问地址为 http://{host}:{port}/{context-path}/ws/orderService.wsdl @Bean(name = "orderService") public DefaultWsdl11Definition orderServiceWsdl() { DefaultWsdl11Definition wsdlDef = new DefaultWsdl11Definition(); // 对应WSDL中定义的portType名称 wsdlDef.setPortTypeName("OrderPort"); // 实际SOAP请求的接收地址 wsdlDef.setLocationUri("/ws/order-service"); // 对应WSDL的目标命名空间 wsdlDef.setTargetNamespace("http://example.com/order/def"); // 绑定本地WSDL文件 wsdlDef.setWsdl(new ClassPathResource("wsdl/orderService.wsdl")); return wsdlDef; } // 第二个WSDL对应的服务,依次类推添加多个Bean即可 @Bean(name = "userService") public DefaultWsdl11Definition userServiceWsdl() { DefaultWsdl11Definition wsdlDef = new DefaultWsdl11Definition(); wsdlDef.setPortTypeName("UserPort"); wsdlDef.setLocationUri("/ws/user-service"); wsdlDef.setTargetNamespace("http://example.com/user/def"); wsdlDef.setWsdl(new ClassPathResource("wsdl/userService.wsdl")); return wsdlDef; } }
- 配置Spring-WS核心Servlet,绑定服务根路径:
public class SoapServletInitializer extends AbstractAnnotationConfigMessageDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return null; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{SoapServiceConfig.class}; } // 所有SOAP请求、WSDL获取请求的统一前缀 @Override protected String[] getServletMappings() { return new String[]{"/ws/*"}; } }
- 编写业务处理端点,对应CXF
EndpointImpl中绑定的业务实现类:
@Endpoint public class OrderServiceEndpoint { private static final String ORDER_NAMESPACE = "http://example.com/order/def"; // 匹配WSDL中定义的getOrder请求 @PayloadRoot(namespace = ORDER_NAMESPACE, localPart = "getOrderRequest") @ResponsePayload public GetOrderResponse handleGetOrder(@RequestPayload GetOrderRequest request) { // 此处编写业务处理逻辑 GetOrderResponse response = new GetOrderResponse(); // 填充返回参数 return response; } }
3 与CXF EndpointImpl的能力对应关系
EndpointImpl.publish(url)指定服务发布地址:对应Spring-WS中DefaultWsdl11Definition.setLocationUri()+ Servlet路径映射的组合EndpointImpl.setWsdlURL()绑定WSDL文件:对应DefaultWsdl11Definition.setWsdl()配置EndpointImpl.setImplementor()绑定业务实现类:对应@Endpoint注解的业务处理端点类
4 多WSDL扩展说明
新增WSDL服务时仅需要新增两个配置即可:
- 新增一个
DefaultWsdl11Definition类型的Bean,绑定对应WSDL文件和服务路径 - 新增对应
@Endpoint注解的业务处理类,匹配对应命名空间的请求
内容的提问来源于stack exchange,提问作者Suresh
相关产品推荐
相关产品推荐

