Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unmarshal a SOAP Fault in a FaultMessageResolver?

I'm new to Spring-WS, and slowly getting my head wrapped around it. Right now, I'm writing a simple client to communicate with an existing WS. I'm using the WebServiceTemplate and the marshalSendAndReceive method. Everything works fine.

However, when a SOAP fault occurs, the WST throws a SoapFaultClientException. I noticed that I can create my own FaultMessageResolver to check what is contained within the SOAP fault. However, when I try to unmarshal the WebServiceMessage in my FaultMessageResolver, I get the following error message:

JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Fault"). Expected elements are ....

Obviously, my unmarshaller is not properly configured. Do I have to generate the JAXB fault model myself using xjc to then be able to unmarshal the error? I am somewhat amazed that this does not already exist.

Is there a better way to extra my custom error information from within my soap:fault response? My error looks something like the following and I am trying to extract/access the serviceErrors item.

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Server</faultcode>
         <faultstring>Blaze Data Error</faultstring>
         <detail>
            <ns14:serviceErrors xmlns="http://www.nb.com/ClientServices/LendingSimulation/CalculateProfitabilityRequest" xmlns:ns13="http://www.nb.com/fw/errorMgmt" xmlns:ns14="http://www.nb.com/ClientServices/LendingSimulation/V1" >
               <ns13:faultstring>ServiceExecutionError</ns13:faultstring>
               <ns13:serviceError>
                  <ns13:errorCode>C10F1013</ns13:errorCode>
                  <ns13:errorType>B</ns13:errorType>
                  <ns13:errorMessage>Unable to retreive additional data</ns13:errorMessage>
                  <ns13:fieldName>Message error received from PHClient :  [An unexpected error code was received : system=PH context=[empty]]</ns13:fieldName>
                  <ns13:systemId>Calculator</ns13:systemId>
                  <ns13:time>2012-06-19 14:45:10.151-0400</ns13:time>
               </ns13:serviceError>
            </ns14:serviceErrors>
         </detail>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

Thanks!

Eric

like image 462
Eric B. Avatar asked Jun 19 '12 20:06

Eric B.


People also ask

What is SOAP fault code?

A SOAP fault is an error in a SOAP (Simple Object Access Protocol) communication resulting from incorrect message format, header-processing problems, or incompatibility between applications.

What is WSDL fault?

Fault messages defined in the WSDL file are called declared faults . Fault messages that are not defined in the WSDL file are called undeclared faults . The process for generating a fault message is implementation dependent and typically depends on whether the fault is declared or not.


1 Answers

I'm using org.springframework.ws.client.core.WebServiceTemplate. I run into the same issue with Fault unmarshalling. I resolved this problem by setting WebServiceTemplate property checkConnectionForFault to false. So in my case it was likely caused by the server not returning status 500 for the fault.

Then spring-ws parsed the fault and passed it to the default FaultMessageResolver and I got a regular SoapFaultClientException to catch.

But that got me only the message of the fault so I had to also add a custom FaultMessageResolver and extract the message along the same lines:

SoapFaultDetail faultDetail = ((SoapMessage) message).getSoapBody().getFault().getFaultDetail();
Iterator<SoapFaultDetailElement> detailEntries = faultDetail.getDetailEntries();
StringBuilder errorMessage = new StringBuilder();

while (detailEntries.hasNext()) {
    SoapFaultDetailElement detailElement = detailEntries.next();
    Node node = ((DOMResult) detailElement.getResult()).getNode();
    CustomServiceFault serviceFault = (CustomServiceFault) marshaller.unmarshal(new DOMSource(node));
    errorMessage.append(serviceFault.getErrorCode() + ": " + serviceFault.getErrorMessage());
}
throw new CustomException(errorMessage.toString());

The marshaller is the same as the one used for the WebserviceTemplate.

like image 156
Ondrej Burkert Avatar answered Nov 02 '22 23:11

Ondrej Burkert