我有一个
ExceptionMapper
Provider
处理我所有的异常。
很明显,我的类实现了
ExceptionMapper<Throwable>
我们知道,所有的例外都是扩展的
Throwable
不知怎么的。
现在我用jax-rs
@NotNull
为了检查我的资源输入值是否不为null,通过一些搜索,我意识到它将抛出
ConstraintViolationException
所以我尝试处理它,并在ExceptionMapper中添加一些细节来响应(添加一些自定义json模型),如下所示:
@Provider
public class AllExceptionMapper implements ExceptionMapper<Throwable> {
private final Logger logger = LogManager.getLogger(AllExceptionMapper.class);
@Override
public Response toResponse(Throwable ex) {
Response response;
CustomError error;
// handle some errors
else if (ex instanceof ConstraintViolationException) {
error = new CustomError(
324, // predefined error type in our documents
"some details"
);
response = Response.status(Response.Status.BAD_REQUEST).entity(error).build();
}
// handle some other errors
return response;
}
}
问题是
ExceptionMapper<ConstraintViolationException>
在那里处理,没有任何问题。
正如我之前所说(也检查过)所有的异常都是从
可抛出
不知怎的,我错过了什么?为什么它不起作用?
......
我的意思是它忽略了我的映射器(从
)并且有正常的行为,即返回状态码400,没有响应负载,就像根本没有映射器一样