Setting Character Set for Jersey Web Services

When developing RESTful Web services using Jersey you will at some point be forced to define/change the character set and content type of your response. Setting the content type is simply done by using the Produces tag, like so:

@Produces(MediaType.APPLICATION_JSON);

At this point it comes in handy to know that Jersey uses UTF-8 as default encoding for response data. BUT it does not specify it explicitely in the response header, which might lead to unexpected results.

So, to make sure target browsers are directed to use your character encoding specifiy it explicitely in your Jersey service. How? Simply add it to the content type as you always do with HTTP headers:

@Produces(MediaType.APPLICATION_JSON + "; charset=utf-8");

That’s all it takes. Easy, right?

You may also like...

2 Responses

  1. aioobe says:

    But this doesn’t change the charset of the response. The title of your post is a bit misleading without this information.

    • Hi aioobe,

      the title does not say “changing character set for Jersey Web Services” but setting it.

      In case you want to convert strings based on a different character set this might not be what you need. There are numerous ways to do so.

      Thanks for your feedback.

      Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *