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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With