首页 文章

为什么拦截器在Apache CXF中的ExceptionMapper之后正常执行?

提问于
浏览
0

我已经实现了一些 RESTful web services 与一些进出 Interceptors 使用 Apache CXF (version 2.7.5) . 我还实现了 ExceptionMapper<NotFoundException> 以捕获访问不存在的资源并向用户返回自定义响应的情况 .

ExceptionMapper

@Provider
public class MyExceptionMapper implements ExceptionMapper<NotFoundException>{
    @Override
    public Response toResponse(NotFoundException exception) {
        Response response = Response
                .status(Status.NOT_FOUND)
                .type(MediaType.APPLICATION_JSON)
                .entity("{\"message\":\"The resource that you tried to access does not exist.\"}").build();
        return response;
    }
}

我注意到当我访问不存在的资源时,我的异常映射器被调用并构建一个自定义 Response ,这很好 . 我的问题是,在调用异常映射器之后,我的拦截器正在被执行,这是我想要避免的 .

是否有任何方法可以在拦截器中识别 MyExceptionMapper 已被调用并调用 return

谢谢

1 回答

  • 0

    在CXF使用ExceptionMapper之后,它将通过正常的传出拦截器链发送消息,而不是故障拦截器链 .

    一种解决方案可能是在ExceptionMapper中的CXF消息Exchange上设置一个值,例如:

    PhaseInterceptorChain.getCurrentMessage()
    .getExchange().put("com.test.skip", Boolean.TRUE);
    

    然后,您可以在拦截器中检索并测试该值 .

    Boolean skip = (Boolean) PhaseInterceptorChain.getCurrentMessage()
    .getExchange().get("com.test.skip");
    

相关问题