解决方法:
1、 在控制器处理方法上的@RequestMapping注解中配置produces
@RequestMapping(value="/getrolelist.html",produces={"application/json;charset=UTF-8"}) ???@ResponseBody ???public Object getRoleList(){ ???????HashMap<String,List<Role>> resultMap=new HashMap<String, List<Role>>(); ???????List<Role> roleList=roleService.getRoleList(); ???????resultMap.put("roleList",roleList); ???????for(Role role:roleList){ ???????????logger.info("角色id"+role.getId()+role.getRoleName()); ???????} ???????????????return JSONArray.toJSONString(roleList);//把resultMap转换成JSON格式返回 ???}
2、 在springMVC里配置StringHttpMessageConverter
???<mvc:annotation-driven> ???????<mvc:message-converters> ???????????<bean class="org.springframework.http.converter.StringHttpMessageConverter"> ???????????????<property name="supportedMediaTypes"> ???????????????????<list> ???????????????????<value>application/json;charset=UTF-8</value> ???????????????????</list> ???????????????</property> ???????????</bean> ???????</mvc:message-converters> ???</mvc:annotation-driven>
3、 1、2都试过了还是乱码,修改@RequestMapping value属性后缀名,试过几个后缀名,除了.html,都能正常返回中文
//修改value后缀名 ???????@RequestMapping(value="/getrolelist.do",produces= ???????{"application/json;charset=UTF-8"}) ???@ResponseBody//修改value后缀名 ???@RequestMapping(value="/getrolelist",produces={"application/json;charset=UTF-8"}) ???@ResponseBody
估计跟@ResponseBody返回方式有关,先记着了解后再补充
JSON 数据传递中文乱码问题
原文地址:https://www.cnblogs.com/corexy/p/8642759.html