Controller返回json的编码处理

不久前在Spring mvc的框架体系下,js端发送ajax请求时,获取的结果为json时会出现中文乱码。

经排查是由于我的spring3.2.0 配置问题。

在Controller端返回的json结果需要进行Jackson的处理。

涉及到的jar包:

  jackson-core、jackson-databind、jackson-annotation

spring-servlet.xml配置添加:

<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);
                }
            }
            
            
            );

效果:








每一步脚印都要扎得深一点!
原文地址:https://www.cnblogs.com/bloodthirsty/p/4110312.html