users@jersey.java.net

[Jersey] Re: Controlling headers, status etc.

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Fri, 9 Aug 2013 00:03:03 +0200

One more thing: in a Servlet container, you should be able to directly inject HttpServletResponse
and set the headers on it within your resource method.

~Jakub

On Aug 8, 2013, at 11:36 PM, Jakub Podlesak <jakub.podlesak_at_oracle.com> wrote:

> Hi Simon,
>
> Please see inline...
>
> On Aug 8, 2013, at 12:53 PM, Simon Roberts <simon_at_dancingcloudphotography.com> wrote:
>
>> Hi all,
>>
>> It seems that the proper/obvious way to control the headers and status code etc that get sent back to the caller from a JAX-RS resource is to have that method return a Response object, and use the ResonseBuilder to interact with those items.
>
> Right.
>
>>
>> However, a team member has suggested that it's more readable (at least in the resulting API documentation) if the method simply returns the object that will form the basis of the returned entity. Is there a way to control things like headers and status using one of the injectables? Is there an @Context, for example?
>
> One way of doing this would be to inject ContainerRequestContext into your resource,
> set a property on it inside your resource method (setProperty method), and then to introduce a ContainerResponseFilter
> in your app, that would work out headers in the response based on the property(ies) value.
>
> Something like:
>
> @Override
> public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
> responseContext.getHeaders().add("Header-1",requestContext.getProperty("Header-1"));
> }
>
> HTH,
>
> ~Jakub
>
>>
>> Many thanks,
>> Simon
>>
>