CXF Interceptors, SoapFaults, and Custom Exceptions

I am trying to change my SOAP webservice in CXF / Spring to use interceptors to capture SoapFaults and convert them to custom exceptions. The goal here is to present the web client with only instances of my custom exception class, when things go wrong, never SoapFault.

This is what I have managed to scrape off so far. You think there will be more correct error handling in various books and books, okay.

FormsEndpointException

@XmlAccessorType(XmlAccessType.FIELD)
public class FormsEndpointException extends Exception
{
    @XmlElement(name="errMsg")
    private String myMsg
...

      

FormsEndpointImpl

@WebService( endpointInterface="mycorp.forms.web.endpoint.FormsEndpoint")
@org.apache.cxf.interceptor.InInterceptors (interceptors = {"mycorp.forms.web.resolver.MyExceptionInterceptor"})

public class FormsEndpointImpl implements FormsEndpoint
{
    @Resource
    WebServiceContext context;

    FormTemplateService  formTemplateService;   //injected from bean

    // must have default constructor per JAXB
    public FormsEndpointImpl() {}

    //only need setters
    public void setFormTemplateService(FormTemplateService formTemplateService) {
        this.formTemplateService = formTemplateService;
    }

    //methods    
    @WebMethod public void insertFormTemplate( FormTemplate formTemplate) throws FormsEndpointException
    {
        try {
            formTemplateService.persist(formTemplate);
        } catch ( Exception e) {
            throw new FormsEndpointException( e.getMessage());
        }
    }
    ....
}

      

MyExceptionInterceptor

public class MyExceptionInterceptor extends AbstractSoapInterceptor
{
    //special constructor
    public MyExceptionInterceptor()
    {
        super( Phase.USER_LOGICAL);
    }

    @Override
    public void handleMessage(SoapMessage arg0) throws Fault
    {
        //left blank for normal stuff    
    }

    //only gets called when unwinding a chain when an exception is called on that chain
    // Every exception will be wrapped into a Fault object by CXF
    @Override
    public void handleFault(SoapMessage arg0)
    {
        Fault aNewFault = new Fault( arg0.getContent(Exception.class));
        aNewFault.setFaultCode( new QName("SNAP"));
        aNewFault.setMessage("DAMMIT");
        // TODO Auto-generated method stub
        // Throwable FormsEndpointException = new FormsEndpointException("Awww");
    }
}

      

context.xml applications

...
<!-- Spring manage ServiceBean -->
<bean id="formsEndpoint" class="mycorp.forms.web.endpoint.FormsEndpointImpl">
  <property name="formTemplateService"  ref="formTemplateService" />
</bean>

<!-- JAX-WS Service Endpoint -->    
<jaxws:endpoint id="formsServiceService" implementor="#formsEndpoint" address="/formsService">
  <jaxws:inFaultInterceptors>
    <ref class="mycorp.forms.web.resolver.MyExceptionInterceptor" />
  </jaxws:inFaultInterceptors>
  <jaxws:outFaultInterceptors>
    <ref class="mycorp.forms.web.resolver.MyExceptionInterceptor" />
  </jaxws:outFaultInterceptors>
</jaxws:endpoint>
...

      

The question is, what do you need to go to handleFault () to create a new instance of MyException?

TIA,

Continuing education Steve

+3


source to share





All Articles