处理来自Spring的WebServiceTemplate.marshalSendAndReceive的错误响应

h43kikqp  于 2022-12-10  发布在  Spring
关注(0)|答案(1)|浏览(95)

我正在使用Spring的WebServiceTemplate.marshalSendAndReceive(Object, WebServiceMessageCallback)方法,我很难理解如何正确处理错误。现在我正在调用一个SOAP服务,如果有人使用相同的唯一标识符在内部发生错误,该服务将以下面的消息响应。

<SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Fault>
        <faultcode>BEA-380001</faultcode>
        <faultstring>Internal Server Error</faultstring>
        <detail>
            <ns0:Fault xmlns:ns0="http://www.package.com/CommonEntities/V2" xmlns:ns2="http://www.package.com/package/schema">
                <ns0:FaultDateTime>2014-01-22T10:39:56.056-06:00</ns0:FaultDateTime>
                <ns0:FaultMessage>An application already exists for ID [987654321].</ns0:FaultMessage>
                <ns0:FaultCode>500</ns0:FaultCode>
                <ns0:FaultStack>com.package.package.exceptions.MyException: An application already exists for ID [987654321].
        at com.package.package.endpoints.MyEndpoint.createObject(MyEndpoint.java:71)
        at com.package.package.endpoints.MyEndpoint$$FastClassByCGLIB$$b46dae63.invoke(&lt;generated&gt;)
        at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
        ...More of this stack trace...

但是,只有将org.springframework日志设置为trace时才能找到此消息。实际上,使它退出对marshalSendAndReceive的调用的消息是以下更通用的SoapFaultClientException消息。

<SOAP-ENV:Body>
      <SOAP-ENV:Fault>
         <faultcode>SOAP-ENV:Client</faultcode>
         <faultstring xml:lang="en">Internal Server Error</faultstring>
         <detail>
            <ns2:Fault xmlns:ns2="http://www.package.com/package/schema" xmlns:ns3="http://www.package.com/package/schema/update">
               <ns2:FaultDateTime>2014-01-22T10:39:56.787-06:00</ns2:FaultDateTime>
               <ns2:FaultMessage>Internal Server Error</ns2:FaultMessage>
               <ns2:FaultCode>2</ns2:FaultCode>
               <ns2:FaultStack>org.springframework.ws.soap.client.SoapFaultClientException: Internal Server Error
    at org.springframework.ws.soap.client.core.SoapFaultMessageResolver.resolveFault(SoapFaultMessageResolver.java:37)
    at org.springframework.ws.client.core.WebServiceTemplate.handleFault(WebServiceTemplate.java:776)
    at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:602)
    at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:539)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:386)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:380)
    at com.package.package.package.adapters.MyAdapter.submitObject(MyAdapter.java:48)

如何从原始响应中获取Body\Fault\details\Fault\FaultMessage并在适配器中将其作为异常抛出?
来自后端请求的实际错误消息将比一般的SoapFaultClientException和堆栈跟踪有用得多。

使用搜索和替换修改了包、类和对象名称以提取个人数据。如果包或类名称看起来很奇怪,这就是原因。

q7solyqu

q7solyqu1#

您可以实作CclientInterceptor类别,以允许自订的客户端消息拦截。
查看本教程:https://codenotfound.com/spring-ws-log-client-server-http-headers.html

相关问题