zoukankan      html  css  js  c++  java
  • 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);
                    }
                }
                
                
                );

    效果:








    每一步脚印都要扎得深一点!
  • 相关阅读:
    ValueError:Expected more than 1 value per channel when training, got input size torch.Size([1, 512, 1, 1])
    指定GPU运行
    ModuleNotFoundError: No module named 'past'
    ImportError: libSM.so.6: cannot open shared object file: No such file or dir
    ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory
    zsh: command not found: conda
    TypeError: Class advice impossible in Python3. Use the @implementer class decorator instead.
    数据结构(八)图
    数据结构(二)线性表
    数据结构(七)数与二叉树
  • 原文地址:https://www.cnblogs.com/bloodthirsty/p/4110312.html
Copyright © 2011-2022 走看看