gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:37:20 29 4
gpt4 key购买 nike

我有一个 Spring MVC bean,我想通过设置编码 UTF-8 来返回土耳其字符。但是虽然我的字符串是“şŞğĞİıçÇöÖüÜ”,但它返回为“??????çÇöÖüÜ”。而且当我查看响应页面(即 Internet Explorer 页面)时,编码是西欧 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;
}

最佳答案

我想通了,你可以添加到请求映射中产生 = "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

关于java - Spring MVC 中的 UTF-8 编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5649329/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com