我在我的Annotation驱动的Spring MVC Java Web应用程序中运行在jetty Web服务器上(目前在maven jetty插件中).
我试图用一个控制器方法做一些AJAX支持,只返回String帮助文本.资源采用UTF-8编码,字符串也是如此,但我的服务器响应是随附的
content-encoding: text/plain;charset=ISO-8859-1
Run Code Online (Sandbox Code Playgroud)
即使我的浏览器发送
Accept-Charset windows-1250,utf-8;q=0.7,*;q=0.7
Run Code Online (Sandbox Code Playgroud)
我正在以某种方式使用弹簧的默认配置
我发现了一个提示将这个bean添加到配置中,但我认为它只是没有使用,因为它说它不支持编码,而是使用默认编码.
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
</bean>
Run Code Online (Sandbox Code Playgroud)
我的控制器代码是(请注意,此响应类型的更改对我不起作用):
@RequestMapping(value = "ajax/gethelp")
public @ResponseBody String handleGetHelp(Locale loc, String code, HttpServletResponse response) {
log.debug("Getting help for code: " + code);
response.setContentType("text/plain;charset=UTF-8");
String help = messageSource.getMessage(code, null, loc);
log.debug("Help is: " + help);
return help;
}
Run Code Online (Sandbox Code Playgroud) 我有一个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)