在一个业务案例中,我试图发送一个让SoapFault响应放置的异常。
出于业务需要,我每次都以不同的HTTP状态发送该故障响应。
我的问题是:
我使用“ HttpResponseInterceptor”更改响应代码,然后使用“ afterCompletion”方法更改“ ClientInterceptor”以更改HTTP状态。
WebServiceTemplate webServiceTemplate = new WebServiceTemplate(webServiceMessageFactory) {
@Override
protected Object handleError(WebServiceConnection connection, WebServiceMessage request) throws IOException {
HttpResponse httpResponse = ((HttpComponentsConnection) connection).getHttpResponse();
String statusAndCode = httpResponse.getStatusLine().getReasonPhrase() + " - "
+ httpResponse.getStatusLine().getStatusCode();
Charset charset = httpResponse.getEntity().getContentEncoding() != null
&& httpResponse.getEntity().getContentEncoding().getValue() != null ? Charset
.forName(httpResponse.getEntity().getContentEncoding().getValue()) : Charset.defaultCharset();
String response = httpResponse.getEntity() != null ? StreamUtils.copyToString(httpResponse.getEntity()
.getContent(), charset) : "N/A";
throw new SoapFaultException(statusAndCode);
}
};
SOAP错误:
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<env:Fault>
<env:Code>
<env:Value>env:Receiver</env:Value>
</env:Code>
<env:Reason>
<env:Text xml:lang="en"><![CDATA[Error communicating with upstream server. Exception:[org.springframework.ws.client.WebServiceTransportException: [Status: Payment Required - 402]
SOAPUI中的HTTP状态
SOAPAction
Accept application/soap+xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection close
#status# HTTP/1.1 500
Content-Length 4625
Date Wed, 10 Jul 2019 07:50:17 GMT
Content-Type application/soap+xml;charset=utf-8