今天在做项目的时候用@ResponseBody注解,发现返回页面上的中文是乱码,经过几番乱战,终于解决了!!
原因是因为之前我们都是在jsp中发送post请求,而在此处直接使用@ResponseBody转JSON的话,默认是发送get请求,就算添加response.setContentType("text/json")和response.setCharacterEncoding("utf-8")也于事无补, 所以一直都是乱码。
特此记录下解决办法。目前有下面几种解决方案:
@RequestMapping的produces方法
第一种解决方案是使用@RequestMapping注解的produces方法。写法如下:
在方法上加上这个注解就可以了。但是这样写的话有限制,只能在特定的方法上面使用。如果需要全局都使用的话,需要修改SpringMVC的配置文件。
使用messageConverters
第二种解决办法是使用HttpMessageConverter接口的相关实现类。我们先看配置文件中的配置信息。
昨天让我郁闷的是,我这样配置了之后确依然是不生效。后来才发现是位置放的不对,需要把这段配置放到<mvc:annotation-driven />的上面。真是很无语的感觉!!!
注意:一定要放到<mvc:annotation-driven />的上面,否则不会生效。
使用<mvc:message-converters>
还有一种方式是在SpringMVC的配置文件中的<mvc:annotation-driven>中加入<mvc:message-converters>的配置。具体配置内容如下:
注意:始用这种配置的时候,需要去掉RequestMappingHandlerMapping、RequestMappingHandlerAdapter或者DefaultAnnotationHandlerMapping、AnnotationMethodHandlerAdapter的Bean配置,要不然可能会不生效。
另外:对于请求映射处理类返回类型可以是String也可以是Object(如果Object是JavaBean的话,SpringMVC会用Jackson来转换成json字符串).例子如下:
@ResponseBody
@RequestMapping(value="/toPerBirthday.do")
public String getIndexPerBasicInfo(HttpServletRequest request,
HttpServletResponse response,ModelMap model) throws Exception{
response.setContentType("text/json");
response.setCharacterEncoding("utf-8");
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
//查询人员生日信息
List<PerBasicInfo> pBasicInfos = personBasicInfoService.findPerBirthdayDay();
List<Map<String, Object>> brithdayList = new ArrayList<Map<String, Object>>();
for(PerBasicInfo pb : pBasicInfos){
Map<String, Object> perBaiscMap = new HashMap<String, Object>();
String birthdayStr = myFormatter.format(pb.getBirthday());
Calendar cToday = Calendar.getInstance(); // 存今天
Calendar cBirth = Calendar.getInstance(); // 存生日
cBirth.setTime(myFormatter.parse(birthdayStr)); // 设置生日
cBirth.set(Calendar.YEAR, cToday.get(Calendar.YEAR)); // 修改为本年
int days;
if (cBirth.get(Calendar.DAY_OF_YEAR) < cToday.get(Calendar.DAY_OF_YEAR)) {
// 生日已经过了,要算明年的了
days = cToday.getActualMaximum(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);
days += cBirth.get(Calendar.DAY_OF_YEAR);
} else {
// 生日还没过
days = cBirth.get(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);
}
perBaiscMap.put("name", pb.getName());
perBaiscMap.put("birthday", birthdayStr);
perBaiscMap.put("time", birthdayStr.substring(5, 7).concat(".").concat(birthdayStr.substring(8, 10)));
if (days == BasicConstants.DAY_ZERO) {
perBaiscMap.put("msg","今天是".concat(pb.getName()).concat("老师的生日") );
}else if (days == BasicConstants.DAY_ONE) {
perBaiscMap.put("msg", "明天是".concat(pb.getName()).concat("老师的生日"));
}else if (days == BasicConstants.DAY_TWO) {
perBaiscMap.put("msg", "后天是".concat(pb.getName()).concat("老师的生日"));
}
brithdayList.add(perBaiscMap);
}
System.out.println(brithdayList);
System.out.println(JSONArray.toJSONString(brithdayList));
model.addAttribute("sasas", JSONArray.toJSONString(brithdayList,true));
Map<String, Object> perBaiscMap1 = new HashMap<String, Object>();
perBaiscMap1.put("msg","今天是".concat("sasas").concat("老师的生日"));
return successJson(perBaiscMap1);