Spring Boot Web服务失败,带有mustUnderstand标头:{http://www.w3.org/2005/08/addressing}Action

时间:2016-01-20 16:08:50

标签: java spring web-services spring-boot ws-addressing

使用此处提供的示例https://spring.io/guides/gs/producing-web-service/我从提供的WSDL文件构建了一个Web服务。如代码所示,支持SOAP12所需的服务就完成了。

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

        servlet.setMessageFactoryBeanName("soap12");

        return new ServletRegistrationBean(servlet, "/myEvents/*");
    }

    @Bean 
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema mySchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("EventService");
        wsdl11Definition.setLocationUri("/myEvents");
        wsdl11Definition.setTargetNamespace("http://event");
        wsdl11Definition.setSchema(ctipsSchema);

        return wsdl11Definition;
    }
    @Bean
    public XsdSchema mySchema() {
        return new SimpleXsdSchema(new ClassPathResource("mySchema.xsd"));
    }

    @Bean(name = "soap12")
    public SaajSoapMessageFactory soap12MessageFactory() throws SOAPException {
        SaajSoapMessageFactory factory = new SaajSoapMessageFactory(MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL));
        return factory;
    } 


    // -- NO longer needed 
    @Override
    public void addInterceptors(List<EndpointInterceptor> interceptors){
        PayloadValidatingInterceptor validatingInterceptor = new PayloadValidatingInterceptor();
        validatingInterceptor.setXsdSchema(ctipsSchema());
        validatingInterceptor.setValidateRequest(true);
        validatingInterceptor.setValidateResponse(true);
        interceptors.add(validatingInterceptor); 
        try {
            interceptors.add(wss4jSecurityInterceptor());
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }

    @Bean
    public Wss4jSecurityInterceptor wss4jSecurityInterceptor() throws Exception{
        Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
        interceptor.setValidationActions("UsernameToken");
        interceptor.setValidationCallbackHandler(new Wss4jSecurityCallbackImpl());

        return interceptor;
    }
}

我面临的问题是我收到了与mustUnderstand相关的错误

SoapMessageDispatcher  : Could not handle mustUnderstand headers: 
{http://www.w3.org/2005/08/addressing}Action,
{http://www.w3.org/2005/08/addressing}To. Returning fault

我正在研究以下解决方案,但我无法让它发挥作用。

public static final String SOAP_HEADER_ACTION = "Action";
public static final String SOAP_HEADER_TO = "To";
public static final String NS_WS_ADDRESSING =    "http://www.w3.org/2005/08/addressing";
public static final String PREFIX = "wsa";

@Bean(name = "soap12")
public SaajSoapMessageFactory soap12MessageFactory() throws SOAPException {

    MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); 
    SOAPMessage request = messageFactory.createMessage();
    SOAPHeaderElement header = request.getSOAPHeader().addHeaderElement(new QName(NS_WS_ADDRESSING, SOAP_HEADER_ACTION, PREFIX));
    header.setMustUnderstand(true);
    header = request.getSOAPHeader().addHeaderElement(new QName(NS_WS_ADDRESSING, SOAP_HEADER_TO, PREFIX));
    header.setMustUnderstand(true);

    SaajSoapMessageFactory factory = new SaajSoapMessageFactory(messageFactory);
    MessageContext context = new DefaultMessageContext(new SaajSoapMessage(request), factory);

    return factory;
}

Endpoint类是

@Endpoint 
public class HeartbeatEndpoint {
    private static final String NAMESPACE_URI = "http://myevent";

    @Action("http://myevent/Heartbeat") 
    @ResponsePayload
    public HeartbeatResponse Heartbeat(@RequestPayload Heartbeat arg) {
        HeartbeatResponse response = new HeartbeatResponse();
        response.setHeartbeatResult(5);
        return response;
    }
}

是否有人熟悉如何使用Spring Boot解决此问题?

***端点更新为正确****

0 个答案:

没有答案