users@jersey.java.net

Re: [Jersey] Jersey and Charsets

From: Charles Overbeck <coverbec_at_pacbell.net>
Date: Tue, 10 Aug 2010 10:31:04 -0700 (PDT)

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