CXF拦截器,SoapFaults和自定义异常

时间:2014-10-22 13:35:01

标签: web-services cxf

我正在尝试将基于CXF / Spring的SOAP Web服务修改为使用拦截器 捕获SoapFaults并将其转换为自定义异常。这里的目标是 当出现问题时,只提供我的自定义异常类的实例的Web客户端, 从来不是SoapFault。

到目前为止,这是我设法凑到一起的。你认为还有更多 在各种图书和书籍中进行适当的错误处理,哦。

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中

...
<!-- 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>
...

问题是,什么需要进入handleFault()来抛出MyException的新实例?

TIA,

仍在学习史蒂夫

0 个答案:

没有答案