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

我們提供的服務(wù)有:網(wǎng)站設(shè)計制作、網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、象山ssl等。為1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學管理、有技術(shù)的象山網(wǎng)站制作公司
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)的適配器和映射器標簽中添加兩個轉(zhuǎn)換器即可,可解決對json數(shù)據(jù)請求和響應(yīng)的亂碼(如果tomcat編碼沒改,依然存在亂碼問題,所有出現(xiàn)亂碼是多方面的的)。
以下是配置spinngmvc中帶的兩個json轉(zhuǎn)換器,實現(xiàn)解決json數(shù)據(jù)請求和響應(yīng)亂碼問題。
<!-- 注解的適配器和映射器 -->
<mvc:annotation-driven>
<mvc:message-converters>
<!--@ResponseBody 中文響應(yīng)亂碼 -->
<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ù)亂碼的解決方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀。
本文標題:java中json傳輸數(shù)據(jù)亂碼的解決方法
分享地址:http://www.chinadenli.net/article44/gcshee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、做網(wǎng)站、品牌網(wǎng)站設(shè)計、品牌網(wǎng)站建設(shè)、企業(yè)建站、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)