I am using Jersey Guice and need to configure a custom ExceptionMapper
My module looks like this:
public final class MyJerseyModule extends JerseyServletModule
{
@Override
protected void configureServlets()
{
...
filter("/*").through(GuiceContainer.class);
...
}
}
And this is my ExceptionMapper:
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
public class MyExceptionMapper implements ExceptionMapper<MyException>
{
@Override
public Response toResponse(final MyException exception)
{
return Response.status(Status.NOT_FOUND).entity(exception.getMessage()).build();
}
}
Your ExceptionMapper must be annotated with
@Providerand be a Singleton.Then just bind the
ExceptionMapperin one of the Guice modules in the sameInjectorwhere yourJerseyServletModule, and Jersey Guice will find it automatically.You can also directly bind it in the
JerseyServletModuleif you want to: