处理Spring的WebServiceTemplate.marshalSendAndReceive的故障响应

时间:2014-01-22 17:01:48

标签: java spring soapfault webservicetemplate

我正在使用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和堆栈跟踪更有用。

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

1 个答案:

答案 0 :(得分:0)

您可以实现CclientInterceptor类,该类允许自定义客户端消息拦截。

查看本教程: https://codenotfound.com/spring-ws-log-client-server-http-headers.html