温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java中json传输数据乱码的解决方法

发布时间:2020-06-10 09:20:08 来源:亿速云 阅读:311 作者:Leah 栏目:编程语言

本篇文章主要探讨java中json传输数据乱码的解决方法。有一定的参考价值,有需要的朋友可以参考一下,跟随小编一起来看解决方法吧。

1、对参数先进行ISO-8859-1编码,再以utf-8解码

    @RequestMapping(method=RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<ResultModel> searchBorrows(String borrow_name) 
    throws UnsupportedEncodingException{
           //解决乱码问题
         System.out.println("编码前===:"+borrow_name);//乱码
         String borrowName=new String(borrow_name.getBytes("ISO-8859-1"),"utf-8");
         System.out.println("编码后:========="+borrowName);//正常

2、如果是一般的请求,(非ajax的json**请求乱码**,直接在web.xml中配置中文过滤器) 如下:

<filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern><!-- 对请求项目下所有资源进行过滤-->
    </filter-mapping>

在没有用springmvc时,也可添加该句解决post请求的乱码问题:request.setCharacterEncoding(“UTF-8”);
注: tomcat8已经把get请求的乱码问题解决了,tomcat7还需自己解决

3、ajax的json数据乱码

在项目中有时需要异步请求,可以在springmvc配置文件中,在注解实现的适配器和映射器标签中添加两个转换器即可,可解决对json数据请求和响应的乱码(如果tomcat编码没改,依然存在乱码问题,所有出现乱码是多方面的的)。

以下是配置spinngmvc中带的两个json转换器,实现解决json数据请求和响应乱码问题。

<!-- 注解的适配器和映射器 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <!--@ResponseBody 中文响应乱码 -->
            <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>

                        <value>
                            application/json;charset=UTF-8
                        </value>
                        <value>
                            application/x-www-form-urlencoded;charset=UTF-8                     
                        </value>
                    </list>
                </property>
            </bean>


<!-- JSON中文请求乱码及解决 
HttpMediaTypeNotAcceptableException: Could not find acceptable representation  异常信息-->
            <bean id="jacksonMessageConverter"
                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>

看完这篇文章,你们学会java中json传输数据乱码的解决方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI