Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception Mapper not getting invoked in JAX-RS jerey

I am trying to configure an ExceptionMapper that will catch all the 404 related exception in my application. This is the first time I am trying t play around with this ExceptionMapper, hence facing lots of issue, might be missing something silly :(

Below is what I did in the ExceptionMapper class:

public class ClassNotFoundMapper implements ExceptionMapper<NotFoundException> {

@Override
public Response toResponse(NotFoundException ex) {
    return Response.status(404).entity(ex.getMessage()).type("text/plain").build();
}

}

In web.xml I added this entry:

<init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>
            com.service.rest;
            com.service.exception
        </param-value>
    </init-param>

In the service I did this:

@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(); 
}

The exception present in the ExceptionMapper is not getting invoked.

Please let me know what am I missing.

like image 473
WhoAmI Avatar asked Nov 21 '25 12:11

WhoAmI


1 Answers

You're missing the @Provider annotation on your ClassNotFoundMapper.

like image 59
Joe Avatar answered Nov 23 '25 02:11

Joe