Java Http请求传json数据乱码问题的解决
程序员文章站
2022-06-14 13:43:54
业务场景:调easyui的dialog打开一个弹窗,传参是用json封装的,而且有中文,然后在极速模式是正常的,在ie11测试发现中文出现乱码了
var para...
业务场景:调easyui的dialog打开一个弹窗,传参是用json封装的,而且有中文,然后在极速模式是正常的,在ie11测试发现中文出现乱码了
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数据,并转成list,然后在ie模式,获取到的json数据都出现中文乱码,然后导致json不能解析报错了
string params = request.getparameter("params"); logger.info("文书参数转换前:》》"+params); params=stringescapeutils.unescapejava(params); logger.info("文书参数转换后:》》"+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加密来传,然后发现base64加密中文出现问题,所以改成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();
后台代码修改,解码一遍,然后发现在ie也正常
string params = request.getparameter("params"); logger.info("文书参数转换前:》》"+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("文书参数转换后:》》"+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(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。