溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java中json傳輸數(shù)據(jù)亂碼的解決方法

發(fā)布時間:2020-06-10 09:20:08 來源:億速云 閱讀:332 作者:Leah 欄目:編程語言

本篇文章主要探討java中json傳輸數(shù)據(jù)亂碼的解決方法。有一定的參考價值,有需要的朋友可以參考一下,跟隨小編一起來看解決方法吧。

1、對參數(shù)先進行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已經(jīng)把get請求的亂碼問題解決了,tomcat7還需自己解決

3、ajax的json數(shù)據(jù)亂碼

在項目中有時需要異步請求,可以在springmvc配置文件中,在注解實現(xiàn)的適配器和映射器標簽中添加兩個轉換器即可,可解決對json數(shù)據(jù)請求和響應的亂碼(如果tomcat編碼沒改,依然存在亂碼問題,所有出現(xiàn)亂碼是多方面的的)。

以下是配置spinngmvc中帶的兩個json轉換器,實現(xiàn)解決json數(shù)據(jù)請求和響應亂碼問題。

<!-- 注解的適配器和映射器 -->
    <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傳輸數(shù)據(jù)亂碼的解決方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI