users@jersey.java.net

Re: [Jersey] ResponseBuilder and Response questions

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 27 Jun 2008 11:51:01 +0200

On Jun 27, 2008, at 4:27 AM, Kevin Duffey wrote:

> Hey all,
>
> So per another question/answer I had here, I am now returning
> Response instead of String for my GET method. When returning a
> String, it seemed that the String was the entire response that came
> back. Using a basic URLConnection to make the call, I basically
> read the input stream into a String and that was the same String
> value I returned. I could get the status code as well.


BTW you can do this with the Jersey client API as well:

   Client c = Client.create();
   WebResource r = c.resource("http://host/path");
   // Get full response to access status code and meta-ata
   ClientResponse cr = r.get(ClientResponse.class);
   // Get representation
   String content = r.get(String.class);


> But in my Resource code, I need to do some things that may need to
> set the status of a response to different values (not authorized,
> no content, ok, server error, etc). So by using the ResponseBuilder
> and returning the .build() output, I am able to set the status to
> what I want. Where I am confused tho, is that my GET sets the
> "body" header with the xml I return.

What do you mean by "body" header? How are you setting the content?

Are you doing the following?

   return Response.ok("content").build();

Or

  return Response.status(x).entity("content").build();

> But, on my calling side, the result is status code 204 (no
> content), even tho I can grab the "body" header and still get the
> string out. I did set that status to Response.Status.ok as well,
> and yet the output code still shows 204.. unless ok == 204?

ok == 200, no content = 204


> I am fine if this is the way it should be.. I can grab the String
> returned via the body header just fine. I just want to make sure,
> since my API will be a public one, that this is correct or not.
>

It could be a bug, but if you could send some sample code of how you
are building the response it would help.

I did fix a bug recently where Jersey would override the response
status of 200 with 204 if no entity was specified. Now what is built
in response takes precedence and Jersey will not get in the way.

Hope this helps,
Paul.