users@jersey.java.net

Annotations to set custom HTTP response headers?

From: James Ide <lifestream_at_gmail.com>
Date: Mon, 11 Oct 2010 13:00:43 -0700

I have a Jersey+Guice service, and I'd like to set certain headers in the
HTTP response by marking resource class methods with various annotations.
For example, either of these two options would be alright:

@HttpResponseHeaders({"X-FRAME-OPTIONS: SAMEORIGIN"})
// or, alternatively
@FrameOptionsSameOrigin // simply a statically defined version
@GET public String resourceMethod() { ... }

I've spent some time poking around Jersey guides and blogs, as well as some
of the jersey-core source code, but the best solution I found was to define
a new MessageBodyWriter and register it via @Provider. This doesn't really
fit my needs since I would like to use the MBW only when my custom
annotations (e.g. @HttpResponseHeaders) are present. Also, I don't believe
that I can run a custom MBW after the default MBWs; they are not chained
together.

So, it seems like I'm not using the right tool for the job. I'm not
particularly interested in using ResponseBuilders or @Context
HttpServletResponse everywhere if possible, which is why I'm searching for
an annotations-based solution. Would some Jersey/Java EE experts here have
some better solutions in mind?

Thanks,
James