SpringMVC的@ResponseBody返回中文亂碼的緣由是SpringMVC默認處理的字符集是ISO-8859-1,在Spring的org.springframework.http.converter.StringHttpMessageConverter類中能夠看到以下代碼: public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1"); 解決返回中文亂碼的問題有兩種,第一種是局部的,只針對於某個方法的返回進行處理,第二種是全局的,針對於整個項目,以下:html
第一種:在@RequestMapping中添加produces="text/html;charset=UTF-8,如: @RequestMapping(value="/login.do",method=RequestMethod.POST,produces="text/html;charset=UTF-8") @ResponseBody public String login(@RequestParam(value="username") String userName,@RequestParam(value="password") String password){ return JSONMessageUtil.getSuccessJSON("登陸成功"); }spring
第二種:在配置文件中的mvc:annotation-driven中添加以下代碼:mvc
<mvc:annotation-driven ><!-- 消息轉換器 -->app
<mvc:message-converters register-defaults="true">htm
<bean class="org.springframework.http.converter.StringHttpMessageConverter">get
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>io
</bean>class
</mvc:message-converters>登錄
</mvc:annotation-driven>亂碼
<mvc:resources location="/resources/" mapping="/resources/**" />