首頁  >  文章  >  Java  >  java中json傳輸資料亂碼問題

java中json傳輸資料亂碼問題

尚
原創
2019-12-30 09:24:073534瀏覽

java中json傳輸資料亂碼問題

java中json傳輸資料亂碼問題解決方法:(建議:java影片教學

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知識請關注java基礎教學欄位。

以上是java中json傳輸資料亂碼問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn