@Provider
public class JerseyExceptionMapper implements ExceptionMapper<JerseyException> {
@Override
public Response toResponse(JerseyException jerseyException) {
return Response.status(jerseyException.getErrorCode()).
entity(jerseyException.getJsonResponseObj()).
type(MediaType.APPLICATION_JSON).
build();
}
}
The code above has unwanted results when you're using an <error-page>
component in the web.xml
. For example, if my Response.status
is set to 400 and my error-page
component defines an <error-code>
of 400, the web server will redirect the request to the location defined in the web.xml
.
This is obviously not what I want for REST requests. I read another post on StackOverflow that said the reason a request gets diverted to the error-page
is because HttpServletResponse.sendError(400)
is set. That post said if you set HttpServletResponse.setStatus(400)
instead, the error-page
will be ignored.
If that is true, I don't see how it's helpful since I did not implement the Jersey code. The option I'm seeing is to investigate the Response class source code and possibly re-implement the status method or perhaps other Jersey code. Is there a simple option here or something I'm missing?
Essentially, my question is: Given that I'm using Jersey for REST and I'm using error-page in my web.xml
, how can I use the above code while ignoring the error-page
for Jersey code only? Any other code that causes HTTP errors should go to the error-page
. Or is there another solution that doesn't involve error-page
but will work identical to what I want?