使用Spring Boot肥皂Web服务响应标头

时间:2019-04-09 11:57:36

标签: java spring web-services spring-boot soap

我正在开发带有Spring Boot的soap Web服务。在请求标头上,传递了一个消息ID标头,并且在响应中要求它具有与响应标头相同的值。但是,我找不到将标头部分添加到响应消息中的方法。

我已经阅读了消息拦截器,但是在我的情况下,因为标头值是可变的,所以它不起作用。

请协助。预先谢谢你!

<!-- MY REQUEST -->
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://mynamespace.com/">
   <soapenv:Header>
      <ns:HeaderRequest>
         <messageID>1234testID</messageID>
      </ns:HeaderRequest>
   <soapenv:Header>
   <soapenv:Body>
    <!-- Request body content -->
   </soapenv:Body>
</soapenv:Envelope>

<!-- EXPECTED RESPONSE -->
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://mynamespace.com/">
    <soapenv:Header>
        <ns:HeaderReply>
            <messageID>1234testID</messageID>
            <statusCode>200</statusCode>
            <statusDescription>Success</statusDescription>
        </ns:HeaderReply>
    </soapenv:Header>
    <soapenv:Body>
        <!-- The response body -->
    </soapenv:Body>
</soapenv:Envelope>

<!-- CURRENT RESPONSE -->
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://mynamespace.com/">
    <soapenv:Header />
    <soapenv:Body>
        <!-- The response body -->
    </soapenv:Body>
</soapenv:Envelope>

// My web config 
@EnableWs
@Configuration
public class WebConfig extends WsConfigurerAdapter {

    @Bean
    public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(
            ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);

        return new ServletRegistrationBean<MessageDispatcherServlet>(servlet, "/ws/*");
    }

    @Bean(name = "services")
    public Wsdl11Definition defaultWsdl11Definition() {
        SimpleWsdl11Definition wsdl11Definition = new SimpleWsdl11Definition();
        wsdl11Definition.setWsdl(new ClassPathResource("/mywsdl.wsdl"));

        return wsdl11Definition;
    }

    @Bean
    public Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("com.ws.objects");
        return marshaller;
    }

    @Bean
    public WebServiceTemplate webServiceTemplate() throws Exception {
        WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
        webServiceTemplate.setMessageFactory(getMessageFactory());
        webServiceTemplate.setMarshaller(jaxb2Marshaller());
        webServiceTemplate.setUnmarshaller(jaxb2Marshaller());
        webServiceTemplate.setDefaultUri("http://localhost:8080/ws");
        webServiceTemplate.setMessageSender(getMessageSender());
        return webServiceTemplate;
    }

    @Bean
    public SaajSoapMessageFactory getMessageFactory() {
        return new SaajSoapMessageFactory();
    }

    @Bean
    public HttpComponentsMessageSender getMessageSender() {
        return new HttpComponentsMessageSender();
    }

}


// My endpoint
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getInput")
    @ResponsePayload
    public JAXBElement<GetResponse> validationRequest(@RequestPayload Element body, MessageContext context) 
    {
        JAXBElement<GetResponse> result =factory.createGetResponse(new GetResponse());
        return result;
    }

0 个答案:

没有答案