使用 Soap v1.2 而不是默认的 v1.1 使用 spring-ws 生成 SOAP web 服务

Producing SOAP webservices with spring-ws with Soap v1.2 instead of the default v1.1

我目前正在从事 Spring soap 服务器项目。我从 Spring 此处 http://spring.io/guides/gs/producing-web-service/ 的入门指南开始构建基本的 SOAP 服务。

默认的 SOAP 协议是 SOAP v1.1。有没有办法可以通过注释将协议设置为 v1.2?

我在 @Endpoint class 上尝试了 @BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING) 注释,但它似乎不起作用。

我也试过 @Endpoint(value = SOAPBinding.SOAP12HTTP_BINDING) 设置它,但这也不起作用,正如启动日志中所见

INFO --- [nio-8080-exec-1] o.s.ws.soap.saaj.SaajSoapMessageFactory  :
 Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol

当然,如果我 post 向服务器发出 SOAP 请求,我会得到以下错误

2015-01-19 15:50:17.610 ERROR 20172 --- [nio-8080-exec-1] c.sun.xml.internal.messaging.saaj.soap : SAAJ0533: Cannot create message: incorrect content-type for SOAP version. Got application/soap+xml;
 charset=utf-8, but expected text/xml
2015-01-19 15:50:17.611 ERROR 20172 --- [nio-8080-exec-1] c.sun.xml.internal.messaging.saaj.soap   :
 SAAJ0535: Unable to internalize message
2015-01-19 15:50:17.617 ERROR 20172 --- [nio-8080-exec-1] a.c.c.C.[.[.[.[messageDispatcherServlet] :
 Servlet.service() for servlet [messageDispatcherServlet] in context with path [] threw exception [R
equest processing failed; nested exception is org.springframework.ws.soap.SoapMessageCreationException: Could not create message from InputStream: Unable to internalize message; nested exception is com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to internalize message] with root cause

com.sun.xml.internal.messaging.saaj.soap.SOAPVersionMismatchException: Cannot create message: incorrect content-type for SOAP version. Got: application/soap+xml; charset=utf-8 Expected: text/xml
        at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.init(Unknown Source)
        at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.<init>(Unknown Source)
        at com.sun.xml.internal.messaging.saaj.soap.ver1_1.Message1_1Impl.<init>(Unknown Source)
        at com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl.createMessage(Unknown Source)

感谢任何帮助。

谢谢!

您将 Spring-WS 与来自 JAX-WS(包 javax.xml.ws)的注释混合在一起。那行不通的。要配置 Spring-WS 以使用 SOAP 1.2,请添加以下 bean 定义:

@Bean
public SaajSoapMessageFactory messageFactory() {
    SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory();
    messageFactory.setSoapVersion(SoapVersion.SOAP_12);
    return messageFactory;
}

如果messageFactory Bean在单例范围内不是,那么afterPropertiesSet()方法在SaajSoapMessageFactory 未在 bean 实例化时调用。

afterPropertiesSet() 方法设置内部消息工厂。因此,如果您的 messageFactory bean 有自己的作用域,您需要像这样设置内部消息工厂:

@Bean
@Scope("custom-scope")
public SaajSoapMessageFactory messageFactory() {
    SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory();
    messageFactory.messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
    return messageFactory;
}

messageFactory() @Bean 是必须的步骤,但是加

不也是基础吗
wsdl11Definition.setCreateSoap12Binding(true); 

服务定义,生成适当的 soap 1.2 绑定?

无论如何,感谢 Andreas Veithen