解决方法:
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 12 都试过了还是乱码, 修改 @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 返回方式有关, 先记着了解后再补充
来源: http://www.bubuko.com/infodetail-2537904.html