users@jersey.java.net

Re: [Jersey] HTTP response of void method

From: Craig McClanahan <Craig.McClanahan_at_Sun.COM>
Date: Sun, 10 May 2009 20:57:49 -0700

Felipe Gaúcho wrote:
> anyway, is there a way to force the response code ??
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>
When I want to enforce things like a 404 if the underlying entity is not
found, I return a JAX-RS "Response" object from my resource methods,
instead of an entity (which always returns a 2xx status), like this:

    @Path("{id}")
    @DELETE
    public Response deleteThing(@PathParam("id") id) {
        if (thing_exists(id)) {
            delete_thing(id);
            return Response.no_content().build();
        } else {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    }

Craig