users@jersey.java.net

Re: [Jersey] granular control over Http Response status

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Wed, 28 May 2008 17:35:57 -0400

On May 28, 2008, at 4:53 PM, g f wrote:

> Hello all,
> I am new to REST, I have a GET method see below:
>
>
> @GET
> @Path("/get/{arg1}")
> @ProduceMime("application/xml")
> public String getXml(@PathParam("id")
> String id) {
> if(context.equals(null)) {
> throw new UnsupportedOperationException();
> }
> MultivaluedMap<String, String> params =
> context.getTemplateParameters();
> String arg1 = params.getFirst("arg1");
> return fpkifd.getFPKIfd(arg1).toString();
> }
>
> Currently fpkifd.getFPKIfd() method returns a single record from a
> database.
> If there are no records with the arg1 value, I return a xml error
> body:
> <?xml version="1.0" ?>
> <error>
> <message>No IFD with the name dufusss</message>
> <code>404</code>
> </error>
>
> I am trying to return a Http status code of 404 but I cannot seem to
> get anything other than a 200.
> I tried to insert the following:
> Response.status(404).build();
> but this has no effect.

Your method has to return the Response you build, e.g.:

@GET
@ProduceMime("application/xml")
public Response getXml() {
   String returnData = ...
   int statusCode = ...
   return
Response.status(statusCode).entity(returnData).type("application/
xml").build();
}

Marc.

>
> How can I gain more control over the status code?
> This would be easier for the end user to receive an int status code
> in lieu of an xml string error code in the body.
>
> TIA!
>
>

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.