首页 > 代码库 > 解决Spring MVC @ResponseBody返回中文字符串乱码问题
解决Spring MVC @ResponseBody返回中文字符串乱码问题
spring mvc使用的默认处理字符串编码为ISO-8859-1
解决方法:
第一种方法:
对于需要返回字符串的方法添加注解,如下:
@RequestMapping(value="http://www.mamicode.com/user/reg",produces="application/json; charset=utf-8")@ResponseBodypublic 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>
解决Spring MVC @ResponseBody返回中文字符串乱码问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。