我同时使用注释@ResponseBody面向字符编码问题。 如果我使用response.getWriter()。write()方法我没有问题,我看不到最喜欢S土耳其字符,O,I,等等。( 我看到的只是问号代替它们 )
我使用Spring的CharacterEncodingFilter使用UTF-8编码 。
我怎样才能解决这个问题呢? 我一定要改变我的所有注解@ResponseBody使用的方法来response.getWriter()。write()方法 ?
抽样方法:
@RequestMapping(value = "/isScdValid.ajax")
public @ResponseBody String isScdValid(HttpServletRequest request, HttpServletResponse response) throws IOException {
boolean isValid = true; // for sample
// continues...
JSONObject jsonObj = new JSONObject();
jsonObj.put("isValid", isValid);
if(isValid) {
jsonObj.put("username", scd.getUsername());
jsonObj.put("sessionUserId", scd.getUserId());
}
return jsonObj.toString(); // not encoding with UTF-8
// response.getWriter().write(jsonObj.toString()); // works right
}
这里是我的字符编码过滤器:
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
还增加了财产的URIEncoding到Tomcat:
<Connector port="9090" protocol="HTTP/1.1" connectionTimeout="20000"
redirectPort="9443"
URIEncoding="UTF-8" compression="on"
compressableMimeType="text/html,text/xml,text/javascript,text/json,text/css,text/plain,application/javascript,application/json,application/pdf"
/>