48

I made a small Rest webservice using Jersey 1.11. When i call the url that returns Json, there are problems with the character encoding for non english characters. The corresponding url for Xml ("test.xml" makes it utf-8 in the starting xml-tag.

How can I make the url "test.json" return utf-8 encoded response?

Here's the code for the service:

@Stateless
@Path("/")
public class RestTest {   
    @EJB
    private MyDao myDao;

    @Path("test.xml/")
    @GET
    @Produces(MediaType.APPLICATION_XML )
    public List<Profile> getProfiles() {    
        return myDao.getProfilesForWeb();
    }

    @Path("test.json/")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Profile> getProfilesAsJson() {
        return myDao.getProfilesForWeb();
    }
}

This is the pojo that the service uses:

package se.kc.mimee.profile.model;

@XmlRootElement
public class Profile {
    public int id;
    public String name;

    public Profile(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Profile() {}

}
Jojje
  • 1,749
  • 3
  • 25
  • 44
  • possible duplicate of [How to set the charset with JAX-RS?](http://stackoverflow.com/questions/3431996/how-to-set-the-charset-with-jax-rs) – rds May 29 '13 at 21:05

7 Answers7

101

Jersey should always produce utf-8 by default, sounds like the problem is that your client isn't interpreting it correctly (the xml declaration doesn't "make" it utf-8, just tells the client how to parse it).

What client are you seeing these problems with?

Valid JSON is only supposed to be Unicode (utf-8/16/32); parsers should be able to detect the encoding automatically (of course, some don't), so there is no encoding declaration in JSON.

You can add it to the Content-Type like so:

@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
Dmitri
  • 8,999
  • 5
  • 36
  • 43
  • 2
    I was seeing the problem with both my Chrome browser and a rest client from google, but your suggestion fixes the problem :) – Jojje Feb 20 '12 at 12:02
  • 1
    2015 and i still have the problem – Kalpesh Soni May 03 '15 at 01:41
  • I uses jersey 1.19 , My Input is = euzé de, But my service returns = euzé de. I use following variation : @Produces(MediaType.TEXT_HTML+ ";charset=UTF-8") as well as in client side conn.setRequestProperty("Accept", "text/html"+ ";charset=UTF-8"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"+ ";charset=UTF-8"); – Somnath Kadam Dec 17 '15 at 11:08
  • 1
    Dear @Dmitri your solution worked for `@Produces` but did not work for `@Consumes`, What can I do to resolve character encoding error at consuming String in the input parameter? – Hosein Aqajani Aug 31 '19 at 09:00
6

if @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") does not function, then try:

@Produces("application/json;charset=utf-8")

in theory it is the same, but the first option did not work to me

alexcornejo
  • 163
  • 1
  • 12
5

If adding the charset to each and every resource is not an option, maybe the answer to this question, which shows how to enforce a default charset, might be helpful.

Community
  • 1
  • 1
martin
  • 1,185
  • 17
  • 22
5

responseMessage is bean class in which we can send UTF-8 charset in response.

return Response.ok(responseMessage).header("Content-Type", "application/json;charset=UTF-8").build();
ketan
  • 19,129
  • 42
  • 60
  • 98
Prem Kumar
  • 51
  • 1
  • 3
2

Jersey is buggy, when Content-Type application/json is used, it does not detect the unicode JSON encoding automatically as it suppose to, but deserialize the request body with whatever runtime platform encoding is used by you server. Same applies for the response body serialization.

Your client need to explicitly specify UTF-8 charset:

Content-Type: application/json;charset=utf-8
0

You can also try this :

return Response.ok(responseMessage, "application/json;charset=UTF-8").build();
-1

i got the same issue in servlet

kindly use : resp.setContentType("application/json;charset=utf-8");

public static void flashOutput(HttpServletRequest req
            , HttpServletResponse resp
            , String output) {

        try {

            Utils.print2("output flash"+output);

            resp.setContentType("application/json;charset=utf-8");
            PrintWriter pw = resp.getWriter();
            pw.write( new String(output.getBytes("UTF-8")));
            pw.close();
            resp.flushBuffer();

        } catch (Exception e) {
            // TODO: handle exception
        }

    }// end flashOutput
Vinod Joshi
  • 7,696
  • 1
  • 50
  • 51