使用 Spring-WS 路由 WS-Addressing 响应

Posted

技术标签:

【中文标题】使用 Spring-WS 路由 WS-Addressing 响应【英文标题】:Route WS-Addressing response with Spring-WS 【发布时间】:2020-04-05 14:44:26 【问题描述】:

我没有太多使用 SOAP Web 服务的经验,尤其是 WS-Addressing。我正在尝试创建示例,根据 WS-Addressing 规范,来自服务器的响应路由到另一台服务器而不是客户端。但我遇到了一些问题,我将在下面解释。

因此,有 3 个服务:Client、Server 和 Response-Receiver。

这是客户端的配置:

ClientConfiguration.java

@Configuration
public class ClientConfiguration 

    @Bean
    public Jaxb2Marshaller marshaller() 
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("my.rinat.country.gen");
        return marshaller;
    

    @Bean
    public CountryWsClient countryWsClient(Jaxb2Marshaller marshaller) 
        CountryWsClient client = new CountryWsClient();
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    

CountryWsClient.java

public class CountryWsClient extends WebServiceGatewaySupport 
    public GetCountryResponse getCountry(String name) throws URISyntaxException 
        GetCountryRequest request = new GetCountryRequest();
        request.setName(name);
        var callback = new ActionCallback(
                new URI("http://www.rinat.my/country/getCountry"),
                new Addressing10(),
                new URI("http://localhost:8282/ws")
        );
        callback.setReplyTo(new EndpointReference(new URI("http://localhost:8282/ws")));
        return (GetCountryResponse) getWebServiceTemplate().marshalSendAndReceive("http://localhost:8181/ws", request, callback);
    

服务器配置:

WsConfiguration.java

@Configuration
public class WsConfiguration extends WsConfigurationSupport 

    public static final String ACTION = "http://www.rinat.my/country/getCountry";

    @Bean
    public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext) 
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean<>(servlet, "/ws/*");
    

    @Bean(name = "country-ws")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema schema) 
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("CountryPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://www.rinat.my/country/gen");
        wsdl11Definition.setSchema(schema);

        Properties actions = new Properties();
        actions.setProperty("getCountry", ACTION);
        wsdl11Definition.setSoapActions(actions);

        return wsdl11Definition;
    

    @Bean
    @Override
    public AnnotationActionEndpointMapping annotationActionEndpointMapping() 
        var mapping = super.annotationActionEndpointMapping();
        mapping.setMessageSender(new HttpComponentsMessageSender());
        return mapping;
    

    @Bean
    public XsdSchema schema() 
        return new SimpleXsdSchema(new ClassPathResource("xsd/countries.xsd"));
    

CountryEndpoint.java

@Endpoint
public class CountryEndpoint 

    private final Countries countries;

    public CountryEndpoint(Countries countries) 
        this.countries = countries;
    

    @Action(WsConfiguration.ACTION)
    @ResponsePayload
    public GetCountryResponse getCountry(@RequestPayload final GetCountryRequest request) 
        GetCountryResponse response = new GetCountryResponse();
        response.setCountry(this.countries.findCountry(request.getName()));
        return response;
    

而且 Response-Receiver 的配置几乎与 Server 相同,因为它们使用相同的 xsd-schemas:

WsConfiguration.java

@Configuration
public class WsConfiguration extends WsConfigurationSupport 

    @Bean
    public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext) 
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean<>(servlet, "/ws/*");
    

    @Bean(name = "country-ws")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema schema) 
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("CountryPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://www.rinat.my/country/gen");
        wsdl11Definition.setSchema(schema);
        return wsdl11Definition;
    

    @Bean
    public XsdSchema schema() 
        return new SimpleXsdSchema(new ClassPathResource("xsd/countries.xsd"));
    

CountryEndpoint.java

@Slf4j
@Endpoint
public class CountryEndpoint 
    @PayloadRoot(namespace = "http://www.rinat.my/country/gen", localPart = "getCountryResponse")
    @ResponsePayload
    public void getCountry(@RequestPayload final GetCountryResponse response) 
        var country = response.getCountry();
        log.info("Country  with the capital , population , and currency ", country.getName(),
                country.getCapital(), country.getPopulation(), country.getCurrency());
    

你可以在github看到完整的例子

当我发起客户端的 WS-Addressing 请求(通过 /kick REST 控制器)时,Response-Receiver 接收到来自服务器的响应,但由于某些“必须理解标头”问题而无法处理(我不太了解什么是它)。这是 Response-Receiver 的日志:

2019-12-12 10:45:01.433 TRACE 17188 --- [nio-8282-exec-1] o.s.ws.server.MessageTracing.received    : Received request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:To SOAP-ENV:mustUnderstand="1">http://localhost:8282/ws</wsa:To><wsa:Action>http://www.rinat.my/country/getCountryResponse</wsa:Action><wsa:MessageID>urn:uuid:340c83d7-6470-444e-b845-cea844e57400</wsa:MessageID><wsa:RelatesTo>urn:uuid:25e481a1-887b-4b28-a921-8615e1a08d83</wsa:RelatesTo></SOAP-ENV:Header><SOAP-ENV:Body><ns2:getCountryResponse xmlns:ns2="http://www.rinat.my/country/gen"><ns2:country><ns2:name>Poland</ns2:name><ns2:population>38186860</ns2:population><ns2:capital>Warsaw</ns2:capital><ns2:currency>PLN</ns2:currency></ns2:country></ns2:getCountryResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>]
2019-12-12 10:45:01.441  WARN 17188 --- [nio-8282-exec-1] o.s.w.soap.server.SoapMessageDispatcher  : Could not handle mustUnderstand headers: http://www.w3.org/2005/08/addressingTo. Returning fault
2019-12-12 10:45:01.450 TRACE 17188 --- [nio-8282-exec-1] o.s.ws.server.MessageTracing.sent        : Sent response [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:MustUnderstand</faultcode><faultstring xml:lang="en">One or more mandatory SOAP header blocks not understood</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>] for request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:To SOAP-ENV:mustUnderstand="1">http://localhost:8282/ws</wsa:To><wsa:Action>http://www.rinat.my/country/getCountryResponse</wsa:Action><wsa:MessageID>urn:uuid:340c83d7-6470-444e-b845-cea844e57400</wsa:MessageID><wsa:RelatesTo>urn:uuid:25e481a1-887b-4b28-a921-8615e1a08d83</wsa:RelatesTo></SOAP-ENV:Header><SOAP-ENV:Body><ns2:getCountryResponse xmlns:ns2="http://www.rinat.my/country/gen"><ns2:country><ns2:name>Poland</ns2:name><ns2:population>38186860</ns2:population><ns2:capital>Warsaw</ns2:capital><ns2:currency>PLN</ns2:currency></ns2:country></ns2:getCountryResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>]

我将不胜感激。

【问题讨论】:

【参考方案1】:

路由消息的接收者应该实现@Action端点,并且路由器必须发送动作头,其值等于接收者端点的@Action注释的值。

您可以找到here的完整示例。

【讨论】:

以上是关于使用 Spring-WS 路由 WS-Addressing 响应的主要内容,如果未能解决你的问题,请参考以下文章

Spring-WS:具有 WSDL 多节点分类的 SimpleWsdl11Definition

使用 WebSphere、Spring-WS 和 WSS4J 的 WebService 证书验证

@PayloadRoot vs @Action vs @SoapAction 在 Spring-WS 中

Spring-WS WS-Security LDAP 身份验证

Spring-WS为SOAP版本1.2设置SOAPAction头

Spring-ws自动生成WSDL时生成java类的最佳方式