【发布时间】:2014-11-07 07:40:19
【问题描述】:
我想在球衣休息服务中捕获所有意外异常。 因此我写了一个 ExceptionMapper:
@Provider
public class ExceptionMapper implements javax.ws.rs.ext.ExceptionMapper<Exception> {
private static Logger logger = LogManager.getLogManager().getLogger(ExceptionMapper.class.getName());
@Override
public Response toResponse(Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Internal error").type("text/plain").build();
}
}
映射器确实捕获了所有异常。因此我不能写:
public MyResult getById(@PathParam("id")) {
if (checkAnyThing) {
return new MyResult();
}
else {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
}
这被映射器捕获。现在我必须写:
public Response getById(@PathParam("id") {
if (checkAnyThing) { {
return Response.ok().entity(new MyResult()).build();
}
else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
这是捕获所有意外异常并在球衣中返回错误(错误代码)的正确方法吗?或者还有其他(更正确的)方法吗?
【问题讨论】:
标签: java jersey jax-rs jersey-2.0