Маршрутизация ответа WS-Addressing с помощью Spring-WS

У меня не так много опыта работы с веб-службами SOAP и, в частности, с WS-адресацией. Я пытаюсь создать пример, в котором в соответствии со спецификацией WS-Addressing ответ от сервера направляется на другой сервер вместо клиента. Но я столкнулся с некоторыми проблемами, которые я объясню ниже.

Таким образом, есть 3 службы: клиент, сервер и ответ-получатель.

Вот конфигурации клиента:

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"));
    }
}

Конечная точка страны.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 почти идентичны конфигурации сервера, потому что они используют идентичные xsd-схемы:

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"));
    }
}

Конечная точка страны.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 получает ответ от сервера, но не может его обработать из-за какой-то проблемы с заголовками mustUnderstand (я недостаточно понимаю, что это такое). Вот журнал 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/addressing}To. 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>]

Буду признателен за помощь.


person MarkHunt    schedule 12.12.2019    source источник


Ответы (1)


Получатель маршрутизируемого сообщения должен реализовать @Action конечная точка и маршрутизатор должны отправить заголовок действия со значением, равным значению @Action аннотации конечной точки получателя.

Полный пример вы можете найти здесь.

person MarkHunt    schedule 20.12.2019