我正在尝试配置一个 ExceptionMapper,它将捕获我的应用程序中所有与 404 相关的异常。这是我第一次尝试使用这个 ExceptionMapper,因此面临很多问题,可能会遗漏一些愚蠢的东西:(
下面是我在 ExceptionMapper 类中所做的:
public class ClassNotFoundMapper implements ExceptionMapper<NotFoundException> {
@Override
public Response toResponse(NotFoundException ex) {
return Response.status(404).entity(ex.getMessage()).type("text/plain").build();
}
在 web.xml 我添加了这个条目:
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>
com.service.rest;
com.service.exception
</param-value>
</init-param>
在服务中我这样做了:
@GET
@Path("{param}")
public Response getName(@PathParam("param") String msg, @HeaderParam("name") String headerName) {
//SOME CODE HERE AND THEN CONDITIONALLY RETURN THE EXCEPTION
return Response.status(Response.Status.NOT_FOUND).entity("NOT FOUND").build();
}
ExceptionMapper 中存在的异常未被调用。
请让我知道我错过了什么。
最佳答案
您在 ClassNotFoundMapper
上缺少了 @Provider
注释。
关于java - Exceptionmapper 未在 JAX-RS Jersey 中被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15182501/