2012-10-04 28 views
0

我有一台服务器将一组类作为RESTful服务公开。我知道我们可以使用ExceptionMapper将异常传递给客户端。客户端和服务器之间共享的检查异常很少。但是在我的一些服务中,我有几个检查异常,这些异常在客户端JVM中不可用。JAX RS CXF Interceptor用于引发客户端特定异常

我知道,更改端点以确保正确处理checked-exception可以解决问题。

但是,我想做到这一点,在拦截器层,原因有二:

  • 这将是一个地方,我可以处理所有导致检查的异常的调用。
  • 由于目前的发布日期,这将是一个很大的重构工作。在CXF documentation

看,我明白我必须扩展AbstractPhaseInterceptor并覆盖handleMessage()

public class MyOutExceptionInterceptor extends AbstractPhaseInterceptor<Message> { 

    public AttachmentInInterceptor() { 
     //Which phase to call here ?? 
     super(Phase.POST_INVOKE); 
    } 

    public void handleMessage(Message message) { 

     //Check from message that it contains an exception of MyCheckedException.class 
     //Create an exception that client can understand 

    } 
} 

我该怎么办呢?

在此先感谢。

回答

0

尝试是这样的:(我省略了制图/在detaisl填充为简洁;关键的事情做的是替换邮件内容)

public void handleMessage(Message message) { 
// Map exception 
    Exception exception = message.getContent(Exception.class); 
    Exception mappedException = mapper.map(exception); 

    Fault fault = exception instanceof Fault ? (Fault) exception : null; 
    if (fault == null) 
    { 
     fault = new Fault(exception); 
     message.setContent(Exception.class, fault); 
    } 
    fillInFaultDetails(fault, exception); 
} 

这实际上是从我们的代码片段。至于阶段 - 我们在POST_LOGICAL运行它; POST_INVOKE也可以工作。

顺便说一句 - 不知道什么是你的使用情况,但我不喜欢使用故障通信业务异常(在我的脑海故障指示一般的消息处理错误,而不是一个业务逻辑异常

+0

感谢您的回复。但是,我在'fault = new Fault(exception)'中得到了一个RuntimeException;'在带有此代码的链式拦截器中。 – Chris

+0

这是什么样的例外? – mucher

1

我。知道我来晚了,但我也有这个问题,这种解决方案提出了这样以供将来参考:

覆盖handleFault代替,里面:

Exception fault = message.getContent(Exception.class); 
Exception exception = fault.getCause(); 
YourOwnFault newFault = new YourOwnFault("bla bla bla"); 
message.setContent(Exception.class, newFault); 

换句话说,提取故障,拿到作为原因的异常,创造e新的错误并插入int。

+0

你没有使用'exception'变量。你为什么这么做? –

+0

可能在YourOwnFault的构造函数中使用它,而不是“bla bla bla bla”。 – Gunslinger