Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get SOAP fault's faultcode when handling custom fault exception

Our system consumes SOAP Web Service, using JAX-WS client stubs generated based on service's WSDL. In case of error server returns SOAP faults like this:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <s:Fault>
      <faultcode>SomeErrorCode</faultcode>
      <faultstring xml:lang="en-US">Some error message</faultstring>
      <detail>
        <ApiFault xmlns="http://somenamespace.com/v1.0" xmlns:a="http://somenamespace.com/v1.0" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
          <a:RequestId>123456789</a:RequestId>
          <a:CanRetry>true</a:CanRetry>
        </ApiFault>
      </detail>
    </s:Fault>
  </s:Body>

Based on WSDL SomeCustomFault exception class is generated and all service methods are declared to throw this (see below) exception.

@WebFault(name = "ApiFault", targetNamespace = "http://services.altasoft.ge/orders/v1.0")
public class SomeCustomFault
    extends Exception
{
    private ApiFault faultInfo;

    public SomeCustomFault(String message, ApiFault faultInfo) {
        super(message);
        this.faultInfo = faultInfo;
    }

    public SomeCustomFault(String message, ApiFault faultInfo, Throwable cause) {
        super(message, cause);
        this.faultInfo = faultInfo;
    }

    public ApiFault getFaultInfo() {
        return faultInfo;
    }
}

As you can see this custom fault exception extends Exception and not SOAPFaultException. Hovewer I need to get SOAP fault's faultcode which could be retrieved only from SOAPFaultException using getFaultCode method. Could you tell me how can I reach SOAPFaultException or SOAP fault's faultcode in place where I catch above mentioned custom fault exception?

like image 697
Nikoloz Avatar asked Nov 20 '13 13:11

Nikoloz


People also ask

How does spring boot handle SOAP fault exception?

One way is writing your custom interceptor which implements Spring WS's ClientInterceptor interface. You should override handleFault method to handle SOAP faults with your custom logic. Then you need to register your custom Interceptor class as an interceptor at your SOAP client config class.

What is a SOAP fault exception?

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.

Does SOAP have built in error handling?

The body of the response contains a single Fault element in the SOAP envelope namespace. SOAP uses this mechanism to indicate that an error has occurred and to provide some diagnostic information. There are three child elements. The faultcode element must be present in all cases.

Where are the errors displayed when a SOAP message is being processed?

If an error occurs during processing, the response to a SOAP message is a SOAP fault element in the body of the message, and the fault is returned to the sender of the SOAP message.


1 Answers

You could implement a JAX-WS handler and add it to your client web service reference. This will be given opportunity to handle the request message and response message OR notified of a fault.

Create a SOAPHandler<SOAPMessageContext> and your handleFault() method will be passed the SOAPMessageContext. From that you can getMessage().getSOAPBody().getFault() to get the SOAPFault, which contains getFaultCode() and getDetail().

Assign your new fault handler to your web service ref. One way is via @HandlerChain. It will be invoked prior to your catch clause.

like image 158
Scott Heaberlin Avatar answered Sep 21 '22 08:09

Scott Heaberlin