users@jersey.java.net

Jersey and Charsets

From: Charles Overbeck <coverbec_at_pacbell.net>
Date: Mon, 9 Aug 2010 17:46:31 -0700 (PDT)

Hello,

I'm trying to understand Jersey and charsets. I'm using Jersey 1.1.5.1, with
JAXB and mainly the default Jersey classes for generating XML and JSON
responses, as well as my own MessageBodyWriter that generates PDFs.

1) In my tests, the XML and JSON responses are always UTF-8 encoded. That's
actually the encoding I want. However, the charset is not set in the
Content-Type in the HTTP header. For example, for JSON it reads "Content-Type:
application/json". I think it should ideally be "Content-Type: application/json;
charset=UTF-8". How will a client know the charset otherwise? Or is JSON always
UTF-8? The XML responses don't have the charset either, but I guess that doesn't
really matter, as the encoding is in the XML prolog. Still, it seems like that
would be nice too.

2) I don't need to do this, but in researching this, I couldn't get my Jersey
app to return anything not in UTF-8 encoding. I'm curious why not, and maybe the
answer will help me understand things. For example, if I send an HTTP header
"Accept-Charset: UTF-16", the response still comes back in UTF-8. How do I set
up my app to create responses in different encodings? Or how do I make the HTTP
request so as to get back the data in a different encoding?

To my knowledge I'm doing fairly vanilla stuff. Here's a snippet from one of my
resource files:

    @Path("{id: [0-9]+}")
    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON,
"application/pdf"})
    public JAXBElement<InvoiceType> retrieveInvoice(@PathParam("id") Long id)
throws MyException {
       // Just return a JAXBElement<InvoiceType>, and let MessageBodyWriters do
the rest...

Thanks,

Charles