Controller返回json的编码处理
2014-11-20 13:49
344 查看
不久前在Spring mvc的框架体系下,js端发送ajax请求时,获取的结果为json时会出现中文乱码。经排查是由于我的spring3.2.0 配置问题。在Controller端返回的json结果需要进行Jackson的处理。涉及到的jar包: jackson-core、jackson-databind、jackson-annotationspring-servlet.xml配置添加:
不仅可以解决中文乱码,还可以将json里的换行|r|n去掉;MappingJackson2HttpMessageConverter: 控制@ResponseBody注解返回的json格式。Controller里处理代码如下:
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json; charset=UTF-8</value> <value>application/x-www-form-urlencoded; charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven><mvc:annotation-driven>自动注册: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter所以这两个bean不用追加到drven里了。StringHttpMessageConverter:
不仅可以解决中文乱码,还可以将json里的换行|r|n去掉;MappingJackson2HttpMessageConverter: 控制@ResponseBody注解返回的json格式。Controller里处理代码如下:
@RequestMapping(value="myprofile/base.json",method = RequestMethod.GET) @ResponseBody public String loadSession(Model model, @RequestParam("appid")String appid, @RequestParam("appkey")String appkey, @RequestParam("openid")String openid, @RequestParam("pf")String pf, @RequestParam("openkey")String openkey, HttpServletRequest request, HttpServletResponse response){ response.setHeader("Charset", "UTF-8"); response.setContentType("application/x-www-form-urlencoded; charset=utf-8"); String serverName =Constant.serverName; OpenApiV3 apiV3 =new OpenApiV3(appid, appkey); apiV3.setServerName(serverName); String protocol ="http"; String scriptName="/v3/user/get_info"; String result =getUserInfo(apiV3, scriptName, openid, openkey, protocol,pf); return result; }Js端请求代码如下:
$j.ajax( { type: "get", async: false, url: getUserInfo, contentType: "application/x-www-form-urlencoded; charset=utf-8", data: { 'appid':appid, 'appkey':appkey, 'openid':openid, 'openkey':openkey, 'pf':pf }, dataType: "json", cache: false, success: function (data) { if(data.ret=='1002'){ //登陆失效,需重新登陆 fusion2.dialog.relogin(); }else{ $j("#userinfo").html("用户信息:<br>"+data); } }, error: function (err) { alert(err); } } );效果:
相关文章推荐
- Spring MVC Controller中返回json数据中文乱码处理
- Spring Boot @ControllerAdvice 处理全局异常,返回固定格式Json
- Spring MVC Controller中返回json数据中文乱码处理
- Spring MVC Controller中返回json数据中文乱码处理
- Spring MVC Controller中返回json数据中文乱码处理
- Spring Boot @ControllerAdvice 处理全局异常,返回固定格式Json
- javascript 处理返回json中的\u中文乱码问题(也不是乱码了,就是\u编码)
- Spring MVC Controller中返回json数据中文乱码处理
- spring mvc对视图解析器controller返回数据转换json的处理的相关配置
- Spring MVC Controller中返回json数据中文乱码处理
- (转:Tonny Yang)jQuery备忘之(一):jQuery处理.Net后台返回的Xml格式与Json格式的数据的比较分析
- 使用 ASP.NET 一般处理程序或 WebService 返回 JSON
- jquery调用后台方法返回json数据的处理
- 使用ASP.NET一般处理程序或WebService返回JSON的实现代码
- 使用ASP.NET一般处理程序或WebService返回JSON的实现代码
- jquery mvc ajax json 当返回json的数据为数组对象时。该如何处理?
- 用Jquery处理PHP返回的JSON格式数据的三种方法
- EasyNet.Solr系列:处理xml或者json格式返回数据
- JQuery ajax返回JSON时的处理方式
- Mysql中的数据用 json_decode 解析返回null的一则处理方法