Re: [Jersey] Customize default error responses

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 02 Feb 2010 11:15:11 +0100

Hi Chris,

Jersey will always throw a WebApplicationException for HTTP error-
based responses, and only the status code (and potentially some
headers) will be set, the entity will not be set.

So you can register an ExceptionMapper for WebApplicationException.

JAX-RS/Jersey will re-throw any exception that is not mapped to the

Thus for full mapping i recommend doing the following:

- register an ExceptionMapper<WebApplicationException> to modify
responses, perhaps specific to the HTTP status code.

- register an ExceptionMapper<Throwable> to catch all other
exceptions, perhaps with a generic response signaling a 500
   sever error.


On Jan 30, 2010, at 12:09 AM, Chris Carrier wrote:

> Hey folks,
> I've been poking around but haven't found any threads or docs to
> explain what i'm after. At least not recent ones.
> So basically for our app all our error responses contain a JSON
> message to explain the problem. That is fine for exceptions that
> come from my code because i can just turn them into whatever HTTP
> error I want. My question is can I customize the error body
> returned for HTTP errors that never reach my code. For instance for
> a 415 method not allowed error I want to construct a JSON response
> instead of the default html error. Is it possible to do this? I
> looked at the ExceptionMapper but I'm not sure what exception to map
> since the request is never really reaching my code. I could
> implement some kind of catchall resource that will try to grab all
> requests that aren't supported and creates the response but that
> feels kind of hacky.
> Thanks
> Chris