users@jersey.java.net

Re: [Jersey] ExceptionMapper and _at_QueryParam Objects

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 27 Feb 2009 20:10:32 +0100

On Feb 27, 2009, at 7:16 PM, Chris Jolley wrote:

> I have an @QueryParam Object and am parsing the request param in
> the constructor of my object.
>
> Example:
>
> @Produces({"application/json", "application/xml"})
> public Lists getJsonOrXml(
> @DefaultValue(DEFAULT_VIEW)
> @QueryParam(VIEW_PARAM) ViewParam view)
>
> If the value for my param is malformed I throw a BadRequestException
> and I have an ExceptionMapper set up to capture that exception and
> return a 400. However, the ExceptionMapper never gets invoked. The
> container sends back a 404 instead.
>
> Is this by design? Is there a way to change this behavior?
>
>

The spec states in 3.2 [1]:

   A WebApplicationException thrown during construction of field or
property values using 2 or 3 above is
   processed directly as described in section 3.3.4. Other exceptions
thrown during construction of field or property
   values using 2 or 3 above are treated as client errors: if the
field or property is annotated with @MatrixParam,
   @QueryParam or @PathParam then an implementation MUST generate a
WebApplicationException that wraps
   the thrown exception with a not found response (404 status) and no
entity; if the field or property is annotated
   with @HeaderParam or @CookieParam then an implementation MUST
generate a WebApplicationException
   that wraps the thrown exception with a client error response (400
status) and no entity. The
   WebApplicationException MUST be then be processed as described in
section 3.3.4.

So you need to utilize WebApplicationException or a define sub-class
of for such cases if you want to override the default. You can use an
exception mapper for a sub-class of WebApplicationException to
specifically map instances of the sub-class.

The thoughts behind this were that we did not want any exception
"leaking" out so that it might be mapped to some other incorrect
status code.

Paul.

[1] https://jsr311.dev.java.net/nonav/releases/1.0/spec/
spec3.html#x3-220003.2