users@jersey.java.net

Re: [Jersey] Jersey and Charsets

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Wed, 11 Aug 2010 10:45:44 +0200

Hi Charles,

Perhaps the best way you can add the charset is to write a response
filter to modify the content-type for certain values rather than
modifying all your annotations.

    ContainerResponse response;

    MediaType m = response.getMediaType();
    if (/ charset not present on m and m requires a charset) {
      // create a new media type adding a charset of UTF-8.
      m = ...
      response.getHttpHeaders().putSingle("Content-Type", m);
    }

An alternative is to utilize your own constants for media types.

If you are using the variant support for charset conneg you do not
need to declare the charset on the @Produces. You would obtain the
variant and use the information from that to build a Response.

Paul.


On Aug 10, 2010, at 7:31 PM, Charles Overbeck wrote:

> Hi Paul,
>
> Let me make sure I understand this. Sorry for the slow comprehension
> on my part:
>
> >>Yes, Jersey always defaults to UTF-8 but does not set explicitly
> set the charset parameter (strictly speaking it should).
> One can also explicitly set the charset in media type of the
> @Produces.<<
>
> If I want the charset in the HTTP response, I have to update all my
> @Produces annotations, correct? I currently have 44 of those
> annotations in my project, which is probably isn't that many,
> relatively speaking, but still I'd like to make sure before I do
> it. :) That means my example method would change to:
>
> @Path("{id: [0-9]+}")
> @GET
> @Produces({MediaType.APPLICATION_XML + ";charset=UTF-8",
> MediaType.APPLICATION_JSON + ";charset=UTF-8", "application/
> pdf"}) // Not sure if I want it on application/pdf
> public JAXBElement<InvoiceType> retrieveInvoice(@PathParam("id")
> Long id) throws MyException {
>
> >>If you need to do anything other than media type conneg then you
> have to do it yourself using the Request.selectVariant.
> https://jsr311.dev.java.net/nonav/releases/1.1/javax/ws/rs/core/Request.html
> #selectVariant%28java.util.List%29
> You can create a list of variants for the media types and charsets
> that are supported by the server and the most acceptable variant
> will be selected. That list of variants can be statically created so
> it does not need to have a per-request cost.<<
>
> And if I want to support other charsets, I need to explicitly build
> up a Response:
> @Path("{id: [0-9]+}")
> @GET
> @Produces({MediaType.APPLICATION_XML,
> MediaType.APPLICATION_JSON, "application/pdf"})
> public Response retrieveInvoice(@PathParam("id") Long id) throws
> MyException {
> return ResponseBuilder.variants(staticListOfVariants)....
>
> Combined with my previous question, if I wanted to support n
> charsets, and I also wanted the charset in the HTTP header, I would
> then have to declare all the charsets in the @PRODUCES, correct? For
> example:
>
> @Produces({MediaType.APPLICATION_XML + ";charset=UTF-8",
> MediaType.APPLICATION_XML + ";charset=UTF-16",...
>
> If I'm right on this one, well fortunately, we only want to support
> UTF-8. :) But I want to make sure I understand things if it comes up.
>
> Thanks as always for your great support and responses.
>
> Charles