Spring WS使用其他HTTP状态而不是500发送SOAPFault

时间:2019-07-10 08:13:28

标签: soap http-status-codes spring-ws http-status-code-500 soapfault

在一个业务案例中,我试图发送一个让SoapFault响应放置的异常。

出于业务需要,我每次都以不同的HTTP状态发送该故障响应。

我的问题是:

  1. 每次更改HTTP状态时,Spring WS都会触发“ WebServiceTransportException”异常。
  2. 即使我更改了HTTP状态,它也位于“ WebServiceTransportException”消息中的SOAP Fault内容之内,但在SOAPUi中,HTTP状态为500。每次触发异常时,结果HTTP状态为500,忽略我设置手动的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

0 个答案:

没有答案