users@jersey.java.net

[Jersey] Resource locator question

From: NBW <emailnbw_at_gmail.com>
Date: Thu, 17 Mar 2011 21:58:48 -0400

I was wondering if there was a way, without getting my hands dirty, to
customize the 404 response given when a resource is not located because one
of the QueryParams which is an enum is bad. Let me illustrate the situation:

public enum FooStatus {
    stopped,
    running,
    paused
}

@GET
@Path("/foo/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Foo getFooByIdAndStatus(@PathParam("id"} final int id,
@QueryParam("status") final FooStatus status) {
 ...
}

If someone tries to call /foo/1?status=stopped then the above resource is
located because Jersey maps status into the enum because it is valid.
However, if the call looks like /foo/1?status=bogus Jersey send back a 404
with the default 404 error message because bogus didn't map to a valid enum
value.

In that case I would like to be able to customize the 404 response and take
advantage of Jersey's automatic 'type checking' if you will, however, I am
not interested in say injecting the @Context UriInfo and taking on
the responsibility of type checking I simply want to in certain situation
customize the response.

What I am doing now is using a String for the QueryParam like so:

public String getFooByIdAndStatus(@PathParam("id"} final int
id, @QueryParam("status") final String status) {

and doing the validation of status being a valid enum - catching the
IllegalArgumentException and then customizing the error response. It works
but I am interested in seeing if I can leverage Jersey's 'validation' and
simply customize the response.

Thanks,

-Noah