Search code examples
javasoapcxfinterceptorsoapfault

Catching the SOAP Fault error in custom interceptor (Soap12FaultOutInterceptor)


I wrote a custom CXF interceptor to log all the SOAP request and responses into the database and it seems to be working fine with positive test cases and server errors.

But when a SOAP Fault occurs it just neglects my interceptor and nothing is logged.

Custom CXF interceptors.

public class DbLogOutInterceptor extends AbstractSoapInterceptor {

 public void handleMessage(SoapMessage message) {
    logger.info("Handling outbound request");

    String transaction = MDC.get(mdcKey);
    logger.info("OutBound Transaction ID : {} ", transaction);

     //code to log the SOAP message in database
    .......

     }
   }

I am not seeing the log statements from this method instead I see

 11:56:34,102 INFO  [Soap12FaultOutInterceptor] class org.apache.cxf.binding.soap.interceptor.Soap12FaultOutInterceptor$Soap12FaultOutInterceptor Internalapplication/soap+xml
 11:56:34,103 INFO  [EligibilityInfo] Outbound Message
 ---------------------------
 ID: 2
 Response-Code: 500
 Encoding: UTF-8
 Content-Type: application/soap+xml
 Headers: {}
 Payload :  

What I have to do in order to capture the SOAP fault erros in my custom interceptors.


Solution

  • So in my custom interceptor I write the following code:

    Fault fault = new Fault(message.getContent(Exception.class));
    

    Now this is in some legacy code that was throwing exceptions from Java and letting the framework convert it to a fault. I won't get into my feelings on that, but this will get you the fault that is generated.

    Now if you are throwing a fault from your service method, do

    Fault fault = message.getContect(Fault.class);
    

    Hopefully this will help you get the answer to what you want. Make sure you register the interceptor like below

    <jaxws:endpoint
      id="fooService" implementor="com.bar.FooService" address="/FooServices">
      <jaxws:outFaultInterceptors>
            <ref class="com.bar.interceptor.DbLogOutInterceptor"/>
      </jaxws:outFaultInterceptors>
    </jaxws:endpoint>
    <jaxws:endpoint