java – 在Spring MVC中的UTF-8编码问题

我是一个Spring MVC bean,我想通过设置编码UTF-8返回土耳其字符。但是我的字符串是“şŞğĞİıçÇöÖüÜ”,它返回为“??????çÇöÖüÜ”。并且当我看看响应页面,这是互联网浏览器页面,编码是西欧iso,而不是UTF-8。

这里是代码:

    @RequestMapping(method=RequestMethod.GET,value="/GetMyList")
public @ResponseBody String getMyList(HttpServletRequest request, HttpServletResponse response) throws CryptoException{
    String contentType= "text/html;charset=UTF-8";
    response.setContentType(contentType);
    try {
        request.setCharacterEncoding("utf-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    response.setCharacterEncoding("utf-8");     
    String str="şŞğĞİıçÇöÖüÜ";
    return str;
}   
我想出来了,你可以添加到请求映射produce =“text / plain; charset = UTF-8”

@RequestMapping(value = "/rest/create/document", produces = "text/plain;charset=UTF-8")
@ResponseBody
public void create(Document document, HttpServletRespone respone) throws UnsupportedEncodingException {

    Document newDocument = DocumentService.create(Document);

    return jsonSerializer.serialize(newDocument);
}

see this blog post for more details on the solution

http://stackoverflow.com/questions/5649329/utf-8-encoding-problem-in-spring-mvc

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:java – 在Spring MVC中的UTF-8编码问题