在JAX-RS jerey中没有调用异常映射器

Who*_*AmI 3 java rest web-services jax-rs

我正在尝试配置一个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();
}
Run Code Online (Sandbox Code Playgroud)

}

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>
Run Code Online (Sandbox Code Playgroud)

服务中我这样做了:

@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(); 
}
Run Code Online (Sandbox Code Playgroud)

不会调用ExceptionMapper中出现的异常.

请让我知道我错过了什么.

Joe*_*Joe 6

你错过了你的@Provider注释ClassNotFoundMapper.