users@jersey.java.net

Re: [Jersey] Customize default error responses

From: Robert Koberg <rob.koberg_at_gmail.com>
Date: Tue, 2 Feb 2010 06:08:27 -0800

On Feb 2, 2010, at 2:15 AM, Paul Sandoz wrote:

> 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 container.
>
> 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.


In Tomcat, the only way I could see how to ensure all errors come back as JSON, was to create a Tomcat specific Valve that extends org.apache.catalina.valves.ErrorReportValve. For Tomcat at least, the default error valve is hard coded to write out HTML (not even XHTML).


>
> Paul.
>
>
> 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
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>