我有一个基于JAX-RS的web应用程序。该应用程序包含2种API:
这两个API产生不同的格式响应。
我目前只有一个ExceptionMapper,以捕获异常,如:EJBAccessException、NotAllowedException.
这两种API类型都可以生成。
我的问题是:分离异常处理的最佳方法是哪一种?
(对于rest的相同异常的反应应该会为XML响应生成JSON响应)
发布于 2018-09-14 09:25:39
@Context private ResourceInfo resourceInfo;
@Override
public Response toResponse(Throwable throwable) {
if(resourceInfo.getResourceClass().isAnnotationPresent(RestExceptionMapper.class)) {
return RestExceptionHandler.handle(throwable, servletRequest);
} else if (resourceInfo.getResourceClass().isAnnotationPresent(SoapExceptionMapper.class)) {
SoapExceptionHandler.handle(throwable, servletRequest);
}
return Response.status(500).entity("ERROR").build();
}
这是我的新异常映射器的模板。使用注释链接到资源。
https://stackoverflow.com/questions/52328082
复制相似问题