Spring 配置两个@Endpoint,每个都有一个唯一的wsdl文件

Spring configure two @Endpoint, each with a unique wsdl file

我已经能够获得一个带有 wsdl 文件的 @Endpoint:

@EnableWs
@Configuration
public class EventServerConfiguration extends WsConfigurerAdapter {
    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet, "/event");
    }


    @Bean(name = "wsdl-event")
    public Wsdl11Definition defaultWsdl11Definition() {
        SimpleWsdl11Definition wsdl11Definition = new SimpleWsdl11Definition();
        wsdl11Definition.setWsdl(new ClassPathResource("/wsdl/event.wsdl"));
        return wsdl11Definition;
    }

    @Bean
    AnnotationActionEndpointMapping endpointMapping() {
        AnnotationActionEndpointMapping mapping = new AnnotationActionEndpointMapping();
        return mapping;
    }
}

但是对于我的用例,我有两个需要处理的 wsdl 文件。最初我设置了一个 RestController 并从 xml POST 请求中解组了主体,但我现在想尝试纯粹的 spring 方式。

我假设我需要创建两个 MessageDispatcherServlet,每个 wsdl 定义一个,但我不知道如何正确映射或注册我的端点。

有什么想法吗?

摆弄我创建了副本(具有不同的 wsdl 和 bean 名称)WSDLdefinition bean,并且 messageDispatcherServletServletRegistrationBean 给了我错误 Servlet messageDispatcherServlet was not registered (possibly already registered?) 而我的端点似乎出现了,但是 spring returns 对所述端点的任何请求都是 404。

Spring-ws版本:3

我的假设是每个 Spring @Enpoint class 我需要不同的路径。我现在意识到我只需要注册一个 MessageDispatcherServlet 并定义处理传入请求所需的 wsdl 文件。

因为我需要两个不同的 wsdl 文件,我可以通过创建一个 ServletRegistrationBean 来支持这两个文件,其中包含一个 MessageDispatcherServlet,两个 Wsdl11Definition bean(每个 wsdl 文件一个),以及在我的@Endpoint class 中,我将命名空间和 localPart 设置为 @PayloadRoot 注释的一部分。

配置class

// Configuration class
@EnableWs
@Configuration
public class SpringConfiguration 
{
    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) 
    {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);

        return new ServletRegistrationBean(servlet, "/soap");
    }

    @Bean
    AnnotationActionEndpointMapping annotationActionEndpointMapping() 
    {
        AnnotationActionEndpointMapping mapping = new AnnotationActionEndpointMapping();
        // add any interceptors here for features like logging
        return mapping;
    }

    // wsdl file A
    @Bean(name="wsdl-definition-A")
    public Wsdl11Definition wsdl11DefinitionA()
    {
        SimpleWsdl11Definition def = new SimpleWsdl11Definition();
        wsdl11Definition.setWsdl(new ClassPathResource("wsdl/A.wsdl"));

        return wsdl11Definition;
    }

    // wsdl file B
    @Bean(name="wsdl-definition-B")
    public Wsdl11Definition wsdl11DefinitionA()
    {
        SimpleWsdl11Definition def = new SimpleWsdl11Definition();
        wsdl11Definition.setWsdl(new ClassPathResource("wsdl/B.wsdl"));

        return wsdl11Definition;
    }
}

如果您需要不同的路径而不是一个端点。我 假设 您需要调整上面定义的 ServletRegistrationBean。现在所有内容都映射到 "/soap".

这里是端点:

@Endpoint
public class AServer
{
    private final static String NAMESPACE_URI = "http://some.namespace/v3/idk/am/i/even/real";
    private final static String LOCALPART = "XMLElementName";
    private final SomeMarshaller marshaller;

    @Autowired
    public EventServer(EventReceiveMarshaller marshaller) 
    {
        // I use a marshaller class to simplify my life
        this.marshaller = marshaller; 
    }

    @Override
    @ResponsePayload
    @PayloadRoot(namespace = NAMESPACE_URI, localPart = LOCALPART)
    public JAXBElement<SomeResponseMessageType> triggerResponse(@RequestPayload JAXBElement<SomeRequestMessageType> msg) {

        // do something with the `msg` request

        // Send a OK response
        return this.marshaller.createReply(msg);
    }
}

然后 BServerAServer 完全相同,除了 NAMESPACE_URILOCALPART,因为它们取决于各自的 wsdl 文件。