Spring MVC中的UTF-8编码问题

ysn*_*nky 48 java spring-mvc utf-8 character-encoding

我有一个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;
}   
Run Code Online (Sandbox Code Playgroud)

Cha*_* Wu 64

我已经想通了,你可以添加到请求映射产生="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);
}
Run Code Online (Sandbox Code Playgroud)

有关该解决方案的更多详细信息,请参阅此博文

  • 同样对我来说,“链接博客文章的解决方案1对我有用,即将字节写入输出流。其他两个解决方案对我不起作用。” (2认同)

aki*_*aki 22

在您的调度程序servlet上下文xml中,您必须"<property name="contentType" value="text/html;charset=UTF-8" />"在viewResolver bean上添加属性 .我们正在使用freemarker进行观看.

它看起来像这样:

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
       ...
       <property name="contentType" value="text/html;charset=UTF-8" />
       ...
</bean>
Run Code Online (Sandbox Code Playgroud)


rwi*_*zel 8

自己将JSON字符串转换为UTF-8.

@RequestMapping(value = "/example.json", method = RequestMethod.GET)
@ResponseBody
public byte[] example() throws Exception {

    return "{ 'text': 'äöüß' } ".getBytes("UTF-8");
}
Run Code Online (Sandbox Code Playgroud)

  • 使用`response.getOutputStream()。write(jsonObject.toString()。getBytes(“ UTF-8”));的唯一适用于我的解决方案。 (2认同)

Xob*_*tun 5

在 Spring 5 或者更早的版本中,有MediaType class。如果您想遵循 DRY,它已经有正确的行:

public static final String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";
Run Code Online (Sandbox Code Playgroud)

所以我使用了这组控制器相关的注解:

@RestController
@RequestMapping(value = "my/api/url", produces = APPLICATION_JSON_UTF8_VALUE)
public class MyController {
    // ... Methods here
}
Run Code Online (Sandbox Code Playgroud)

它在文档中被标记为已弃用,但我遇到了这个问题,我认为这比在整个应用程序中的每个方法/控制器上复制粘贴上述行要好。


Tru*_*ung 2

还有一些类似的问题:Spring MVC 响应编码问题自定义 HttpMessageConverter 用 @ResponseBody 来做 Json 的事情

\n\n

然而,我的简单解决方案:

\n\n
@RequestMapping(method=RequestMethod.GET,value="/GetMyList")\npublic ModelAndView getMyList(){\n  String test = "\xc4\x8d\xc4\x87\xc5\xbe\xc4\x91\xc5\xa1";\n  ...\n  ModelAndView mav = new ModelAndView("html_utf8");\n  mav.addObject("responseBody", test);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

和视图 html_utf8.jsp

\n\n
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>${responseBody}\n
Run Code Online (Sandbox Code Playgroud)\n\n

没有额外的类和配置。
\n您还可以为其他内容类型创建另一个视图(例如 json_utf8)。

\n