Java Http請求傳json數(shù)據(jù)亂碼問題的解決
業(yè)務(wù)場景:調(diào)easyui的dialog打開一個彈窗,傳參是用json封裝的,而且有中文,然后在極速模式是正常的,在ie11測試發(fā)現(xiàn)中文出現(xiàn)亂碼了
var params = JSON.stringify(writParamList);
top.dialog({
id: 'noticeList',
title:'列表',
width:900,
height:500,
url:'${root}/notice/multiNoticeList.do?params='+params,
onclose:function(){
//location.reload();
}
}).showModal();
后臺獲取json數(shù)據(jù),并轉(zhuǎn)成list,然后在ie模式,獲取到的json數(shù)據(jù)都出現(xiàn)中文亂碼,然后導(dǎo)致json不能解析報錯了
String params = request.getParameter("params");
logger.info("文書參數(shù)轉(zhuǎn)換前:》》"+params);
params=StringEscapeUtils.unescapeJava(params);
logger.info("文書參數(shù)轉(zhuǎn)換后:》》"+params);
ObjectMapper mapper = new ObjectMapper();
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, ApprWritUnionFormModel.class);
/*List<FormModel> writUnionFormList =
JSON.parseArray(params, FormModel.class);*/
List<FormModel> writUnionFormList=new ArrayList<FormModel>();
try {
writUnionFormList = (List<FormModel>)mapper.readValue(params, javaType);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
針對上面問題,和同事討論,最開始打算base64加密來傳,然后發(fā)現(xiàn)base64加密中文出現(xiàn)問題,所以改成URLEncode的方式,前端加密兩遍,后臺在解密一遍
var params = JSON.stringify(writParamList);
top.dialog({
id: 'noticeList',
title:'列表',
width:900,
height:500,
url:'${root}/notice/multiNoticeList.do?params='+encodeURI(encodeURI(params)),
onclose:function(){
//location.reload();
}
}).showModal();
后臺代碼修改,解碼一遍,然后發(fā)現(xiàn)在ie也正常
String params = request.getParameter("params");
logger.info("文書參數(shù)轉(zhuǎn)換前:》》"+params);
//params=StringEscapeUtils.unescapeJava(params);
// byte[] buffer=Base64Utils.decodeFromString(params);
// params = new String(buffer);
try {
params=URLDecoder.decode(params,"utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
logger.info("文書參數(shù)轉(zhuǎn)換后:》》"+params);
ObjectMapper mapper = new ObjectMapper();
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, ApprWritUnionFormModel.class);
/*List<FormModel> writUnionFormList =
JSON.parseArray(params, FormModel.class);*/
List<FormModel> writUnionFormList=new ArrayList<FormModel>();
try {
writUnionFormList = (List<FormModel>)mapper.readValue(params, javaType);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot報錯Invalid?bound?statement?(not?found)的解決
本文主要介紹了springboot報錯Invalid?bound?statement?(not?found)的解決,遇到這種問題通常是沒有配置好配置文件,下面就來具體介紹一下解決方法,感興趣的可以了解一下2025-03-03
IntelliJ IDEA快速創(chuàng)建getter和setter方法
這篇文章主要介紹了IntelliJ IDEA快速創(chuàng)建getter和setter方法,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
SpringBoot實現(xiàn)MapperScan添加動態(tài)配置(占位符)
這篇文章主要介紹了SpringBoot實現(xiàn)MapperScan添加動態(tài)配置(占位符),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。2022-01-01
一文帶你掌握J(rèn)ava?Future模式的靈活應(yīng)用
Future模式,簡單來說,就是一種能夠管理異步操作的方式,它可以讓咱們的程序在執(zhí)行一個耗時任務(wù)的同時,還能繼續(xù)做其他事情,下面我們就來看看Future模式的具體應(yīng)用吧2024-01-01
java編譯后的文件出現(xiàn)xx$1.class的原因及解決方式
這篇文章主要介紹了java編譯后的文件出現(xiàn)xx$1.class的原因及解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
SWT(JFace) Wizard(Eclipse插件編程必備)
SWT(JFace)小制作:Wizard(Eclipse插件編程必備)2009-06-06
java中判斷字段真實長度的實例(中文2個字符,英文1個字符)
下面小編就為大家?guī)硪黄猨ava中判斷字段真實長度的實例(中文2個字符,英文1個字符)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01

