-->

Spring-WS : Soap fault not identified as Fault

2019-08-10 22:42发布

问题:

I am using Spring-WS, and I am getting a SOAP Fault with error code 500 when calling the web service via marshalSendAndReceive from the webServiceTemplate. Unfortunately, the response is not being recognized as a SOAP fault (it is throwing a WebServiceTransportException). In theory, when there is an error response, WebServiceTemplate throws a WebServiceTransportException if the error code is different to 2xx, unless the http error is 500 and the content is a soap fault. Here is an example from the response I get:

    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>"

Does anyone know why is it happening? Is there a way to just treat it as the SOAP fault it is?

回答1:

The checkConnectionForFault property in WebServiceTemplate should allow you to work around this problem.



回答2:

I digged a little bit into the hasFault method from AbstractHttpSenderConnection (the one that will be called to decide if the error is a soap fault or not), and I see the next code for that method:

/*
 * 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;
}

That means, that there has to be an http header with ContentType = xml, which the web service I am calling is not setting. I have to research now how to set that header in the responses before the error is evaluated in the web service template.