How to force handleFault() to be called

Viewed 321

I had implemented a handler with Apache CXF web service. My intention is to trace the exceptions & log more information. Snippet from handler code is something as follows

public class SoapFaultHandler  implements SOAPHandler<SOAPMessageContext> {

    @Override
    public boolean handleMessage(SOAPMessageContext context) {
       //what should be thrown from here or web service so that handleFault() is called
       

    }
    @Override
    public boolean handleFault(SOAPMessageContext context) {
    }
    //......................
}

I had tried throwing javax.xml.ws.soap.SOAPFaultException() from handleMessage(), but handleFault was not called. Can someone please give some hint?

Edit: The exceptions throw from handleMessage() was not reaching the handleFault() defined in the same handler. However of late I find that the handleFault() gets called when I throw SoapFault exceptions from the web service. Below is a snippet of the code.

 catch(CustomException e)
 { 
   String codeStr =//custom string here
   QName faultCode = new  QName("http://cxf.apache.org/soapfault",codeStr);
   String faultString = e.toString();
   SoapFault soapFault = new SoapFault(faultString,faultCode);
   throw soapFault;
}

In the Java client, this was coming as SOAPFaultException and I had to call <exception_obj>.getCause() to get the CXF SoapFault object.

1 Answers

You can register IN/OUT Fault interceptors in the Endpoint registration.

    @Bean
    public Endpoint awesomeEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), awesomeServiceImpl());

        endpoint.getInFaultInterceptors().add(new AwesomeFaultInInterceptor());
        endpoint.getOutFaultInterceptors().add(new AwesomeFaultOutInterceptor());
            
        endpoint.publish("/awesomeService");
        
        return endpoint;
    }
Related