我的jersey-server有以下Exception Mapper:
@Provider
public class ExceptionResponseMapper implements ExceptionMapper<Throwable> {
Logger logger = Logger.getLogger(ExceptionResponseMapper.class);
@Override
public Response toResponse(Throwable e) {
log.error(e);
Response response = Response.status(500)
.entity(e)
.build();
return response;
}
}
我使用config.register(ExceptionResponseMapper.class);
手动注册此提供程序
除了构造函数中的异常之外,Exception Mapper对我的REST资源类中抛出的异常非常有效。
例如。
@Path("/MyResource")
public class MyResource {
public MyResource() {
throw new RuntimeException();
}
}
不会调用Exception Mapper。我该如何处理这种例外情况?
当您的资源由Spring或CDI创建时,Jersey Context尚未就绪,但我的猜测是(如果您使用的是DI框架)。