我使用的是sping-web-3.2.2,jar
方法一:
在@RequestMapping里面加入produces = "text/html;charset=UTF-8"
@RequestMapping(value = "/configrole", method = RequestMethod.GET, produces = "text/html;charset=UTF-8") public @ResponseBody String configrole() { ...... }
方法二:
因为在StringHttpMessageConverter里面默认设置了字符集是ISO-8859-1
所以拿到源代码,修改成UTF-8并打包到spring-web-3.2.2.jar
public class StringHttpMessageConverter extends AbstractHttpMessageConverter{ public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); .......... }
方法三:
修改org.springframework.http.MediaType它的构造方法的参数,并在applicationContext-mvc.xml 加入配置
public MediaType(String type, String subtype, Charset charset) { super(type, subtype, charset); }
方法四:
直接将org.springframework.http.converter.StringHttpMessageConverter 里面的属性defaultCharset设置成utf-8
上面的几种方法经过测试都是可行的,网上还有其他的办法,后面找到一并加进来。