问题是,如果有人使用表情符号或其他使用代理对的字符发布或发表评论
private void writeJSONResponse(boolean hasApiVersion,HttpServletResponse response,JSONResponse jsonResponse) throws IOException{
JsonMapper jsonMapper = new JsonMapper();
String jsonString = jsonMapper.toJson(jsonResponse);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().println(jsonString);
}
我已将此问题发布为 Emoji are not being encoded correctly for output writer 但没有得到任何答案...
http://www.fileformat.info/info/unicode/char/1f604/index.htm
它以{f0 9f 98 84}的形式发送到服务器,但在响应中它被编码为{ed a0 bd ed b8 84}
真正有趣的是,如果我为servlet启用gzip过滤器,则客户端会正确解码响应。
这只是!!!!!! 这不使用gzip编码修复...
private void writeJSONResponse(boolean hasApiVersion,HttpServletResponse response,JSONResponse jsonResponse) throws IOException{
JsonMapper jsonMapper = new JsonMapper();
String jsonString = jsonMapper.toJson(jsonResponse);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
//response.getWriter().println(jsonString);
byte[] bytes = jsonString.getBytes("UTF-8");
response.getOutputStream().write(bytes);
response.getOutputStream().flush();
}
那么...... Writer从字符串vs getBytes获取字节的方式有什么不对?
谢谢。