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.
You're missing the @Provider annotation on your ClassNotFoundMapper.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With