Re: [Jersey] ViewProcessor - how to set the response content-type

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 19 Feb 2010 18:14:46 +0100

Hi Alex,

I agree this area is not very clear.

Since you are using Jersey, 1.1.5? then with implicit views you can
annotate your resource class with:


or if using explicit views you can do:


The "qs" parameter is used as a priority preference for what the
server states is most acceptable. So it should override the client
side preferences if the declared media type in the above annotations
is an acceptable one. Hope that make sense :-)

Otherwise you can inject the following in the ViewProcessor

  @Context HttpContext hc;

then do in the writeTo method:


and set the header, but *before* you write any bytes out to the
OutputStream (once the first byte is written the headers will be


On Feb 19, 2010, at 5:47 PM, Alex Treppass wrote:

> I've rolled my own ViewProcessor implementation to use the Apache
> Velocity template engine.
> In my writeTo(resolvedPath, viewable, outputstream) method I merge /
> render the template and flush it out, which works fine. Opera and IE
> are kind enough to render the given page as .html, but Chrome likes
> to request text/xml so it seems the server is sending it back as this.
> Is there a way to set the content-type header in Viewables as you
> would with Response.ok("<response>", MediaType.TEXT_HTML)?
> Thanks in advance,
> Alex