Spring-WS:肥皂故障未被识别为故障

时间:2015-07-01 13:45:11

标签: java soap spring-ws

我正在使用Spring-WS,当通过webServiceTemplate中的marshalSendAndReceive调用Web服务时,我收到错误代码为500的SOAP Fault。不幸的是,响应没有被识别为SOAP错误(它抛出WebServiceTransportException)。理论上,当存在错误响应时,如果错误代码与2xx不同,WebServiceTemplate将抛出WebServiceTransportException,除非 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故障?

2 个答案:

答案 0 :(得分:2)

WebServiceTemplate中的checkConnectionForFault属性可以让您解决此问题。

答案 1 :(得分:1)

我从AbstractHttpSenderConnection(将被调用以判断错误是否是soap错误)中的 hasFault 方法中稍微挖掘一下,然后我看到该方法的下一个代码:

/*
 * 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标头,我正在调用的Web服务没有设置。我现在必须研究如何在Web服务模板中评估错误之前在响应中设置该标头。