如何配置Spring WS以生成SOAP 1.2协议消息
解决Spring WS配置SOAP 1.2协议的问题
你遇到的这个错误完全是因为Spring WS默认绑定的是SOAP 1.1协议,它期望请求的Content-Type是text/xml,但SOAP 1.2用的是application/soap+xml; charset=UTF-8,所以才会报类型不匹配的异常。别担心,只需要调整你的XML配置,替换默认的消息工厂就能搞定。
下面是具体的修改步骤和完整配置示例:
首先,在Spring配置文件中添加支持SOAP 1.2的SaajSoapMessageFactory,并指定SOAP版本为1.2:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sws="http://www.springframework.org/schema/web-services" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 核心:配置SOAP 1.2消息工厂 --> <bean id="soap12MessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"> <property name="soapVersion"> <util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/> </property> </bean> <!-- 如果使用注解驱动,一定要指定message-factory-ref指向上面的SOAP1.2工厂 --> <sws:annotation-driven message-factory-ref="soap12MessageFactory"/> <!-- 这里放你原有的其他配置:比如WSDL发布、端点Bean、映射器等 --> <!-- 示例:WSDL发布配置 --> <sws:dynamic-wsdl id="yourService" portTypeName="YourServicePort" locationUri="/services/" targetNamespace="http://your-namespace.com/service"> <sws:xsd location="/WEB-INF/schemas/your-service-schema.xsd"/> </sws:dynamic-wsdl> </beans>
关键要点说明:
SaajSoapMessageFactory是Spring WS处理SOAP消息的核心类,通过soapVersion属性指定为SOAP_12,就能让它处理application/soap+xml类型的请求和响应。- 如果你用了
<sws:annotation-driven>,必须加上message-factory-ref参数,否则Spring还是会用默认的SOAP 1.1工厂,这是很多人踩坑的地方! - 别忘了引入
util命名空间,因为我们用了<util:constant>来引用SOAP版本常量。
配置完成后,再发送SOAP 1.2格式的请求,就不会再出现Content-Type不匹配的异常了,而且你的服务生成的响应也会自动符合SOAP 1.2协议规范。
内容的提问来源于stack exchange,提问作者Mikhail
相关产品推荐
相关产品推荐

