Spring-WS:肥皂故障未识别为故障
Spring-WS : Soap fault not identified as Fault
我正在使用 Spring-WS,当我通过 webServiceTemplate 的 marshalSendAndReceive 调用 Web 服务时,我收到错误代码为 500 的 SOAP 错误。不幸的是,响应没有被识别为 SOAP 错误(它抛出 WebServiceTransportException)。理论上,当有错误响应时,WebServiceTemplate 抛出一个WebServiceTransportException 如果错误代码与2xx 不同,除非 http 错误是500 并且内容是soap 错误。这是我收到的回复中的一个示例:
HTTP/1.1 500 Internal Server Error[\r][\n]"
Content-Length: 887[\r][\n]"
Connection: keep-alive[\r][\n]"
2015-07-01 10:38:22,873 DEBUG [o.s.ws.client.core.WebServiceTemplate] Received error for request [SaajSoapMessage {http://www.example.com/schema/front}use]
2015-07-01 10:38:22,874 DEBUG [org.apache.http.wire] << "<?xml version="1.0" encoding="UTF-8"?>[\n]"
2015-07-01 10:38:22,874 DEBUG [org.apache.http.wire] << "[\n]"
2015-07-01 10:38:22,874 DEBUG [org.apache.http.wire] << "<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">[\n]"
2015-07-01 10:38:22,874 DEBUG [org.apache.http.wire] << " <SOAP-ENV:Body>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <SOAP-ENV:Fault>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <faultcode>SOAP-ENV:Server</faultcode>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <faultstring>Request service/operation version not supported</faultstring>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <faultactor>Server</faultactor>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <detail>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <ns0:serviceException xmlns:ns0="http://www.example.com/facade/interface/v1_1">[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <ns1:message xmlns:ns1="http://www.example.com/common/v1_3">InternalCode01</ns1:messageId>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <ns1:text xmlns:ns1="http://www.example.com/common/v2_1">Request service/operation version not supported</ns1:text>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " </ns0:serviceException>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " </detail>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " </SOAP-ENV:Fault>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " </SOAP-ENV:Body>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << "</SOAP-ENV:Envelope>"
有谁知道为什么会这样?有没有办法将其视为 SOAP 故障?
我深入研究了 AbstractHttpSenderConnection 中的 hasFault 方法(将调用该方法来确定错误是否是 soap 错误),我看到了该方法的下一个代码:
/*
* Faults
*/
public final boolean hasFault() throws IOException {
return HttpTransportConstants.STATUS_INTERNAL_SERVER_ERROR == getResponseCode() && isXmlResponse();
}
/** Determine whether the response is a XML message. */
private boolean isXmlResponse() throws IOException {
Iterator<String> iterator = getResponseHeaders(HttpTransportConstants.HEADER_CONTENT_TYPE);
if (iterator.hasNext()) {
String contentType = iterator.next().toLowerCase();
return contentType.indexOf("xml") != -1;
}
return false;
}
这意味着,必须有一个 ContentType = xml 的 http header,我调用的 Web 服务没有设置它。我现在必须研究如何在 Web 服务模板中评估错误之前在响应中设置 header。
WebServiceTemplate
中的 checkConnectionForFault
属性 应该可以解决这个问题。
我正在使用 Spring-WS,当我通过 webServiceTemplate 的 marshalSendAndReceive 调用 Web 服务时,我收到错误代码为 500 的 SOAP 错误。不幸的是,响应没有被识别为 SOAP 错误(它抛出 WebServiceTransportException)。理论上,当有错误响应时,WebServiceTemplate 抛出一个WebServiceTransportException 如果错误代码与2xx 不同,除非 http 错误是500 并且内容是soap 错误。这是我收到的回复中的一个示例:
HTTP/1.1 500 Internal Server Error[\r][\n]"
Content-Length: 887[\r][\n]"
Connection: keep-alive[\r][\n]"
2015-07-01 10:38:22,873 DEBUG [o.s.ws.client.core.WebServiceTemplate] Received error for request [SaajSoapMessage {http://www.example.com/schema/front}use]
2015-07-01 10:38:22,874 DEBUG [org.apache.http.wire] << "<?xml version="1.0" encoding="UTF-8"?>[\n]"
2015-07-01 10:38:22,874 DEBUG [org.apache.http.wire] << "[\n]"
2015-07-01 10:38:22,874 DEBUG [org.apache.http.wire] << "<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">[\n]"
2015-07-01 10:38:22,874 DEBUG [org.apache.http.wire] << " <SOAP-ENV:Body>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <SOAP-ENV:Fault>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <faultcode>SOAP-ENV:Server</faultcode>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <faultstring>Request service/operation version not supported</faultstring>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <faultactor>Server</faultactor>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <detail>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <ns0:serviceException xmlns:ns0="http://www.example.com/facade/interface/v1_1">[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <ns1:message xmlns:ns1="http://www.example.com/common/v1_3">InternalCode01</ns1:messageId>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " <ns1:text xmlns:ns1="http://www.example.com/common/v2_1">Request service/operation version not supported</ns1:text>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " </ns0:serviceException>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " </detail>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " </SOAP-ENV:Fault>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << " </SOAP-ENV:Body>[\n]"
2015-07-01 10:38:22,875 DEBUG [org.apache.http.wire] << "</SOAP-ENV:Envelope>"
有谁知道为什么会这样?有没有办法将其视为 SOAP 故障?
我深入研究了 AbstractHttpSenderConnection 中的 hasFault 方法(将调用该方法来确定错误是否是 soap 错误),我看到了该方法的下一个代码:
/*
* Faults
*/
public final boolean hasFault() throws IOException {
return HttpTransportConstants.STATUS_INTERNAL_SERVER_ERROR == getResponseCode() && isXmlResponse();
}
/** Determine whether the response is a XML message. */
private boolean isXmlResponse() throws IOException {
Iterator<String> iterator = getResponseHeaders(HttpTransportConstants.HEADER_CONTENT_TYPE);
if (iterator.hasNext()) {
String contentType = iterator.next().toLowerCase();
return contentType.indexOf("xml") != -1;
}
return false;
}
这意味着,必须有一个 ContentType = xml 的 http header,我调用的 Web 服务没有设置它。我现在必须研究如何在 Web 服务模板中评估错误之前在响应中设置 header。
WebServiceTemplate
中的 checkConnectionForFault
属性 应该可以解决这个问题。