spring mvc使用的默认处理字符串编码为ISO-8859-1
解决方法:
第一种方法:
对于需要返回字符串的方法添加注解,如下:
@RequestMapping(value="/user/reg",produces="application/json; charset=utf-8") @ResponseBody public String userReg(HttpServletRequest request,HttpServletResponse response,user u) throws IOException { String msg = "中文乱码解决"; return "{"msg":""+msg+""}"; }
此方法只针对单个调用方法起作用。
第二种方法:
在配置文件中加入
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven>