users@jersey.java.net

Re: [Jersey] Setting Response Status

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 17 Mar 2010 09:22:39 +0100

Hi Charles,

Unfortunately the usual way to do this is to return a Response
instance and then the method signature is not preserved:

   URI u = ...
   Response.created(u).entity(new
GenericEntity<JAXBElement<newlyCreatedCurrency>>(newCurrency) {}).
     build();

Usually a 201 response should have a Location header. If not you can
just do:

   Response.status(201).entity(new
GenericEntity<JAXBElement<newlyCreatedCurrency>>(newCurrency) {}).
     build();


An alternative way if you are just interested in declaring the status
code is to write a resource filter and define your own annotation, say
Status, that you can annotate on your resource method. The resource
filter would check if such an annotation is present and modify the
status code accordingly.

But note as pointed out 201 should also have a Location header, so i
am not sure the 201 created use-case benefits from declaring the
status code.


Note that the reason why GenericEntity is requires is because at the
time we could not work out how to make Response generic, we now know
how to do that, and it is something that should be fixed in any JAX-RS
2.0 effort and an i am inclined to implement a Jersey specific
solution in the interim.

Paul.

On Mar 16, 2010, at 6:24 PM, Charles Overbeck wrote:

> Hello,
>
> I have code like this:
>
> @POST
> @Produces("text/xml")
> @Consumes("text/xml")
> public JAXBElement<CurrencyType> addCurrency(CurrencyType
> newCurrency) {
> ...
> return <JAXBElement<newlyCreatedCurrency>, where
> newlyCreatedCurrency may not be exactly the same as newCurrency>
> }
>
> Because I am doing a create, I want the HTTP status response code to
> be 201, but it is 200.
>
> Is there any way I can set the response code to 201, preserving the
> above method signature?
>
> I know that I can change the method to return a Response, and use
> ResponseBuilder.status() to explicitly set the status. Previously I
> was doing that, and all my method signatures returned Reponses. I've
> since been changing my resource methods to not return Reponses, but
> the actual type of the entity. The code reads better, and there are
> tools that can generate better documentation for my REST API.
>
> Are there just some cases, like this one, where I'm going to have to
> return a Reponse, or is there a way to set the status code here?
>
> Digression: I haven't actually tried any of the documentation tools,
> and maybe Jersey itself offers something, but briefly looked at the
> enunciate web page, which looks like it could generate good doc.
> Does anybody have any experience/recommendations for generating docs
> for their REST API?
>
> Thanks,
>
> Charles
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>