[Jersey] Re: Controlling headers, status etc.

From: Jakub Podlesak <>
Date: Thu, 8 Aug 2013 23:36:43 +0200

Hi Simon,

Please see inline...

On Aug 8, 2013, at 12:53 PM, Simon Roberts <> 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.


> 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:

    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {



> Many thanks,
> Simon