简体   繁体   中英

UTF-8 response encoding with appengine

I need to set the character encoding of the HTTP response to UTF-8.

JsonObject jsonObject;

resp.setContentType("application/json; charset=UTF-8");
//resp.setCharacterEncoding("UTF-8");
PrintWriter out = resp.getWriter();
out.print(jsonObject.toString());

I have try to set encoding on content type, using the specific response method and with system property on appengine-web.xml

<property name="file.encoding" value="UTF-8" />
<property name="DEFAULT_ENCODING" value="UTF-8" />

Every method work fine in in local, but when i deploying my application to App Engine the character encoding goes wrong, and the JSON response String displayed in my browser is not UTF-8. What can I do?

If encoding error is in PrintWriter you can try to write to response not string, but array of characters, as:

out.print(jsonObject.toString().getBytes("UTF-8"));

Otherwise try another library to generate json, such as GSON.

Gson gson = new GsonBuilder().create();
out.print(gson.toJson(Your_Object));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM