您的位置:首页 > 其它

@responsebody 乱码问题

2016-09-27 14:44 99 查看
早上改了个东西,发现返回的json数据出现乱码问题,原因是:

spring mvc使用的默认处理字符串编码为ISO-8859-1,具体参考org.springframework.http.converter.StringHttpMessageConverter

类中public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");

百度的解决办法如下:

解决方法:

第一种:对于需要返回的字符串的方法加注解。如:

@SuppressWarnings("unchecked")
@ResponseBody
@RequestMapping(value="brand",produces = "application/json; charset=utf-8")
public Object brandList(HttpServletRequest request) {
Map<String,Object> map = new HashMap<String,Object>();
PageData pd=new PageData();
try {
//JSONObject json=this.getRequestInfo(request);
String nub=request.getParameter("number").toString();
if(null==nub || nub.equals("")){
map.put("message", "请检查参数");
return AppUtil.returnJson(pd, map);
}else{
String callback = request.getParameter("callback");
if(StringUtils.isNotBlank(callback)){
pd.put("callback", callback);
}
}
Integer number = Integer.parseInt(nub);
map = brandService.getBrandList(number);
} catch (Exception e) {
e.printStackTrace();
map.put("message", "服务器错误");
}
return AppUtil.returnJson(pd, map);
}

我使用了这种方法,此方法只针对单个方法调用起作用。
第二种方法:在application配置文件中加入如下:

<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>

我测试了一下,没用,当然有兴趣的可以去详细测试。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: