You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置两个带独立WSDL文件的Spring @Endpoint?

配置多个带独立WSDL的Spring @Endpoint

我来帮你搞定在Spring中配置两个各自拥有唯一WSDL文件的@Endpoint的问题,其实核心思路就是给每个服务单独定义WSDL配置Bean,同时确保请求能正确路由到对应的Endpoint,下面是具体的实现方案:

1. 核心配置类改造

首先扩展你现有的配置类,为每个服务添加独立的WSDL定义和对应的Schema配置:

@EnableWs
@Configuration
public class MultiWsEndpointConfiguration extends WsConfigurerAdapter {

    // 配置消息分发Servlet,使用通用路径/ws/*,让所有Web服务请求都走这个Servlet
    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        // 替换原来的"/event"为通用路径,支持多个服务
        return new ServletRegistrationBean(servlet, "/ws/*");
    }

    // 第一个服务:Event服务的WSDL配置Bean
    @Bean(name = "eventWsdl")
    public DefaultWsdl11Definition eventWsdl(XsdSchema eventSchema) {
        DefaultWsdl11Definition wsdlDefinition = new DefaultWsdl11Definition();
        wsdlDefinition.setPortTypeName("EventPort"); // WSDL中的端口名称
        wsdlDefinition.setLocationUri("/ws/event"); // 对应Event Endpoint的请求路径
        wsdlDefinition.setTargetNamespace("http://your-domain.com/event"); // 服务的命名空间(要和Endpoint匹配)
        wsdlDefinition.setSchema(eventSchema); // 绑定对应的XSD Schema
        return wsdlDefinition;
    }

    // Event服务的XSD Schema Bean,用来定义请求响应结构
    @Bean
    public XsdSchema eventSchema() {
        return new SimpleXsdSchema(new ClassPathResource("event.xsd")); // 替换为你的XSD文件路径
    }

    // 第二个服务:Another服务的WSDL配置Bean
    @Bean(name = "anotherWsdl")
    public DefaultWsdl11Definition anotherWsdl(XsdSchema anotherSchema) {
        DefaultWsdl11Definition wsdlDefinition = new DefaultWsdl11Definition();
        wsdlDefinition.setPortTypeName("AnotherPort");
        wsdlDefinition.setLocationUri("/ws/another"); // 对应Another Endpoint的请求路径
        wsdlDefinition.setTargetNamespace("http://your-domain.com/another"); // 第二个服务的命名空间
        wsdlDefinition.setSchema(anotherSchema);
        return wsdlDefinition;
    }

    // Another服务的XSD Schema Bean
    @Bean
    public XsdSchema anotherSchema() {
        return new SimpleXsdSchema(new ClassPathResource("another.xsd")); // 替换为第二个服务的XSD文件路径
    }
}

2. 实现两个独立的@Endpoint

每个Endpoint通过@PayloadRoot绑定对应的命名空间,确保请求能正确路由:

Event Endpoint示例

@Endpoint
public class EventEndpoint {
    // 必须和WSDL配置中的targetNamespace一致
    private static final String NAMESPACE_URI = "http://your-domain.com/event";

    // 绑定请求的命名空间和本地元素名称(对应XSD中的请求节点)
    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "EventRequest")
    @ResponsePayload
    public EventResponse handleEventRequest(@RequestPayload EventRequest request) {
        // 这里写你的业务逻辑
        EventResponse response = new EventResponse();
        response.setStatus("SUCCESS");
        return response;
    }
}

Another Endpoint示例

@Endpoint
public class AnotherEndpoint {
    private static final String NAMESPACE_URI = "http://your-domain.com/another";

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "AnotherRequest")
    @ResponsePayload
    public AnotherResponse handleAnotherRequest(@RequestPayload AnotherRequest request) {
        // 第二个服务的业务逻辑
        AnotherResponse response = new AnotherResponse();
        response.setResult("Processed Successfully");
        return response;
    }
}

关键注意事项

  • WSDL访问地址:两个服务的WSDL可以通过以下地址访问:
    • Event服务:http://localhost:8080/ws/eventWsdl.wsdl
    • Another服务:http://localhost:8080/ws/anotherWsdl.wsdl
  • 命名空间匹配:@PayloadRoot的namespace属性必须和对应WSDL配置中的targetNamespace完全一致,否则请求无法路由到对应的Endpoint。
  • 路径隔离:通过setLocationUri为每个服务设置独立的请求路径,避免请求冲突。

内容的提问来源于stack exchange,提问作者andersfylling

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:57:15