users@jersey.java.net

[Jersey] Re: Resource locator question

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Fri, 18 Mar 2011 11:05:04 +0100

Hi Noah,

please see [1]. An example implementation (taken from the Jersey user
guide):

@Provider
public class EntityNotFoundMapper implements
          ExceptionMapper<javax.persistence.EntityNotFoundException> {
      public Response toResponse(javax.persistence.EntityNotFoundException ex) {
          return Response.status(404).
              entity(ex.getMessage()).
              type("text/plain").
              build();
      }
}


~Jakub

[1]http://jsr311.java.net/nonav/releases/1.1/javax/ws/rs/ext/ExceptionMapper.html

On 03/18/2011 02:58 AM, NBW wrote:
> 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
>
>


-- 
Jakub Podles(ák
CZJUG co-lead,
Web Services Research And Development
Oracle, Czech s r.o.
Praha 4, V Parku 8