亲宝软件园·资讯

展开

Java Http请求传json数据乱码问题的解决

人气:0

业务场景:调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();
 }
您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论