在测试的过程中遇到了一个奇怪的异常:
com.sun.xml.internal.messaging.saaj.soap.SOAPVersionMismatchException:
Cannot create message: incorrect content-type for SOAP version.
Got: application/soap+xml;
action="http://smarttrust.com/opm/ws/opm/ActivateRequest";
charset=UTF-8
Expected: text/xml
通过异常信息,可以很明显的发现是携带SOAP消息的HTTP报文请求头的Content-Type字段出了问题
通过抓包可以看到HTTP报文请求头的格式是下面这个样子的(还要注意的一点是,没有SOAPAction字段,而是action字段)
POST /opm HTTP/1.1
Content-Type: application/soap+xml; charset=UTF-8;
action="http://smarttrust.com/opm/ws/opm/ActivateRequest"
User-Agent: Axis2
Host: 10.160.68.107:9010
Transfer-Encoding: chunked
之后我新建了一个测试工程,仔细的查看了下Axis2自动生成的代码,发现有这么一段:
public MathStub(
org.apache.axis2.context.ConfigurationContext configurationContext,
java.lang.String targetEndpoint, boolean useSeparateListener)
throws org.apache.axis2.AxisFault {
populateAxisService();
populateFaults();
_serviceClient = new org.apache.axis2.client.ServiceClient(
configurationContext, _service);
_serviceClient.getOptions().setTo(
new org.apache.axis2.addressing.EndpointReference(targetEndpoint));
_serviceClient.getOptions().setUseSeparateListener(useSeparateListener);
_serviceClient.getOptions().setSoapVersionURI(
org.apache.axiom.soap.SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
}
这部分是用来设置SOAP版本的
_serviceClient.getOptions().setSoapVersionURI(
org.apache.axiom.soap.SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
如果使用SOAP11Constants(SOAP 1.1),HTTP请求头如下:
POST /test HTTP/1.1
Content-Type: text/xml; charset=UTF-8
SOAPAction: "urn:add"
User-Agent: Axis2
Host: 146.11.42.87:9876
Transfer-Encoding: chunked
如果使用SOAP12Constants(SOAP 1.2) ,HTTP请求头如下:
POST /test HTTP/1.1
Content-Type: application/soap+xml; charset=UTF-8; action="urn:add"
User-Agent: Axis2
Host: 146.11.42.87:9876
Transfer-Encoding: chunked
如果不设置这个参数,HTTP请求头如下(和SOAP 1.1是一样的,可以确认默认使用SOAP 1.1):
POST /test HTTP/1.1
Content-Type: text/xml; charset=UTF-8
SOAPAction: "urn:add"
User-Agent: Axis2
Host: 146.11.42.87:9876
Transfer-Encoding: chunked
HTTP响应头中的Content-Type字段也是一样的
参考:
http://stackoverflow.com/questions/8092031/cannot-create-message-incorrect-content-type-for-soap-version-got-text-xml-ch