users@jersey.java.net

Re: [Jersey] Annotations to set custom HTTP response headers?

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Tue, 12 Oct 2010 12:49:04 +0200

Hi James,

You can use a ResourceFilterFactory that introspects the methods on a
resource class and decide to include response filters as appropriate.
See:

   https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/container/filter/package-summary.html

For an example see the source for the RolesAllowedResourceFilterFactory:

https://fisheye4.atlassian.com/browse/jersey/trunk/jersey/jersey-server/src/main/java/com/sun/jersey/api/container/filter/RolesAllowedResourceFilterFactory.java?r=HEAD

Paul.

On Oct 11, 2010, at 10:00 PM, James Ide wrote:

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