即使在收到错误 500 后,SOAP UI 也会显示故障字符串。需要在 JAVA 中查看相同内容

SOAP UI displays faultstring even after error 500 is received. need to view the same in JAVA

故事是这样的:

从 SOAPUI (5.2.1) 发送请求会给我正确的响应,或者在参数无效的情况下,会出现 xml 形式的错误。 查看 SoapUI 日志,这是我所看到的:

Wed Jan 18 16:32:39 EST 2017:DEBUG:Attempt 1 to execute request

Wed Jan 18 16:32:39 EST 2017:DEBUG:Sending request: POST/XBC/services/TranslateGeometryService HTTP/1.1

Wed Jan 18 16:32:48 EST 2017:DEBUG:Receiving response: HTTP/1.1 401 Unauthorized

Wed Jan 18 16:32:48 EST 2017:DEBUG:Connection can be kept alive for 10000 MILLISECONDS

Wed Jan 18 16:32:48 EST 2017:DEBUG:Target requested authentication

Wed Jan 18 16:32:48 EST 2017:DEBUG:Authorization challenge processed

Wed Jan 18 16:32:48 EST 2017:DEBUG:Authentication scope: BASIC 'Spring Security Application'@mywebservice:80

Wed Jan 18 16:32:48 EST 2017:INFO:mywebservice:80 requires authentication with the realm 'Spring Security Application'

Wed Jan 18 16:32:48 EST 2017:DEBUG:Found credentials

Wed Jan 18 16:32:48 EST 2017:DEBUG:Attempt 2 to execute request

Wed Jan 18 16:32:48 EST 2017:DEBUG:Sending request: POST/XBC/services/TranslateGeometryService HTTP/1.1

Wed Jan 18 16:33:14 EST 2017:DEBUG:Receiving response: HTTP/1.1 500 Internal Server Error

Wed Jan 18 16:33:14 EST 2017:DEBUG:Connection shut down

Wed Jan 18 16:33:14 EST 2017:INFO:Got response for [TranslateGeometrySoap11.getLocationFromTramPoleTrackOffset:Request 1] in 35794ms (312 bytes)

(请注意,这 312 个字节出现在错误 500 和连接关闭消息之后)

响应window,同时在SOAPUI中显示:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <SOAP-ENV:Fault>
            <faultcode>SOAP-ENV:Server</faultcode>
            <faultstring xml:lang="en">Error: No Track exists with Track Code: CHS-CSK-UP-TW</faultstring>
        </SOAP-ENV:Fault>
    </SOAP-ENV:Body>

这是完美的,正如我所期望的那样。

现在解决让我发疯的问题。 当我尝试从我的 java 代码发送完全相同的请求时,应该 return 上面的错误,我收到 IOException 说: 服务器 returned HTTP 响应代码:URL 的 500:http://mywebservice/XBC/services/TranslateGeometryService

因此,与 SOAPUI 在其日志中显示的内容几乎相同。

但是...

无论我在代码中做什么,我都无法在 SOAPUI 显示的错误字符串中获取该消息:"No Track exists with Track Code: CHS-CSK-UP-TW".

SOAPUI收到错误500后是怎么做的?或者,更重要的是 - 我如何在收到错误 500 后在 JAVA 中获取它?

尝试使用 SoapFaultException class 和 SoapFault,但我找不到更多的东西。

感谢所有尝试回答此问题的人。

干杯, 皮埃尔

Java 处理程序中的下一个代码使用 SOAPMessageContext 检测 SOAP 错误并从 SOAPBody 中提取错误字符串:

public bool handleFault(SOAPMessageContext context)
{
    try {
        if (context.getMessage().getSOAPBody().hasFault()) {
            SOAPFault fa = context.getMessage().getSOAPBody().getFault();
            System.err.println(fa.getFaultCode());
            System.err.println(fa.getFaultString());
        }
        return true;
    } catch (SOAPException ex) {
        System.err.println("Fault parsing Exception:" + ex.getMessage());
        return true;
    }
    return false;
}