fastjson更新的坑

这次由于业务需求,将项目中的fastjson由1.2.7更新至1.2.56,不料上线后系统间的签名 验签接口 始终过不去,百思不得其解,在网上翻找了很多资料,最后发现原来是在解析的过程中出现了问题,测试代码如下:

public class JsonTest {
    public static void main(String[] args) {
        String strRequestDatagram = "{\"bizContent\":{\"nCallbackState\":0,\"nIncomeTransferState\":0,\"lCompensatoryInterest\":3420,\"nBorrowMode\":85,\"lCompensatoryPenalty\":0,\"nInterestDays\":0,\"lProductId\":0,\"nBorrowerMonthRate\":0,\"lBorrowerId\":24225,\"lBorrowIntentId\":200026131,\"strBorrowerIdentify\":\"430721199512257311\",\"nDebtApplyState\":0,\"strLenderIdentify\":\"0\",\"strLenderName\":\"0\",\"strBorrowConsultNo\":\"\",\"lBorrowerBillId\":200089155,\"strPhoneNO\":\"DHhMsINbOGyXLAvVh1bi2g==\",\"tsRefreshTime\":\"2019-04-10 00:01:01\",\"strProductName\":\"0\",\"nFundYearRate\":0,\"lCompensatoryPrincipal\":16674,\"strUnderName\":\"0\",\"lAmount\":200000,\"nTermIndex\":12,\"strLoanDate\":\"2018-04-09 13:20:49\",\"lId\":74083,\"strBorrowerName\":\"谭淇\",\"nTermCount\":12,\"lCompensatoryAmount\":20094,\"dtCreateTime\":\"2019-04-10 00:01:01\",\"strUnderIdentify\":\"0\",\"lPartnerInterest\":3270,\"strRepayDate\":\"2019-04-09\",\"strIncomeTransferDealSN\":\"\",\"copayState\":0,\"lUnderId\":0,\"strBorrowEndDate\":\"2019-04-09\",\"strReserved\":\"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><CDO><LF N=\\\"lCompensatoryTaskId\\\" V=\\\"4762641\\\"/><LF N=\\\"lBorrowIntentId\\\" V=\\\"200026131\\\"/><LF N=\\\"lBorrowerId\\\" V=\\\"24225\\\"/><LF N=\\\"nBorrowMode\\\" V=\\\"85\\\"/><LF N=\\\"lBorrowerBillId\\\" V=\\\"200089155\\\"/></CDO>\",\"lLenderId\":0},\"orgCode\":\"DF\",\"productId\":\"A0001\",\"requestId\":\"ec75bd009c744fd1b3890b432925d20b\",\"requestTime\":\"2019-04-10 00:03:42\"}";
        System.out.println(strRequestDatagram);

        TreeMap requestDatagram = JSON.parseObject(strRequestDatagram, TreeMap.class);
        System.out.println(JSON.toJSONString(requestDatagram));
    }
}

返回结果如下:

{"bizContent":{"nCallbackState":0,"nIncomeTransferState":0,"lCompensatoryInterest":3420,"nBorrowMode":85,"lCompensatoryPenalty":0,"nInterestDays":0,"lProductId":0,"nBorrowerMonthRate":0,"lBorrowerId":24225,"lBorrowIntentId":200026131,"strBorrowerIdentify":"430721199512257311","nDebtApplyState":0,"strLenderIdentify":"0","strLenderName":"0","strBorrowConsultNo":"","lBorrowerBillId":200089155,"strPhoneNO":"DHhMsINbOGyXLAvVh1bi2g==","tsRefreshTime":"2019-04-10 00:01:01","strProductName":"0","nFundYearRate":0,"lCompensatoryPrincipal":16674,"strUnderName":"0","lAmount":200000,"nTermIndex":12,"strLoanDate":"2018-04-09 13:20:49","lId":74083,"strBorrowerName":"谭淇","nTermCount":12,"lCompensatoryAmount":20094,"dtCreateTime":"2019-04-10 00:01:01","strUnderIdentify":"0","lPartnerInterest":3270,"strRepayDate":"2019-04-09","strIncomeTransferDealSN":"","copayState":0,"lUnderId":0,"strBorrowEndDate":"2019-04-09","strReserved":"<?xml version=\"1.0\" encoding=\"UTF-8\"?><CDO><LF N=\"lCompensatoryTaskId\" V=\"4762641\"/><LF N=\"lBorrowIntentId\" V=\"200026131\"/><LF N=\"lBorrowerId\" V=\"24225\"/><LF N=\"nBorrowMode\" V=\"85\"/><LF N=\"lBorrowerBillId\" V=\"200089155\"/></CDO>","lLenderId":0},"orgCode":"DF","productId":"A0001","requestId":"ec75bd009c744fd1b3890b432925d20b","requestTime":"2019-04-10 00:03:42"}
{"bizContent":{"nCallbackState":0,"nIncomeTransferState":0,"lCompensatoryInterest":3420,"lCompensatoryPenalty":0,"nBorrowMode":85,"nInterestDays":0,"lProductId":0,"lBorrowerId":24225,"nBorrowerMonthRate":0,"strBorrowerIdentify":"430721199512257311","lBorrowIntentId":200026131,"nDebtApplyState":0,"strLenderIdentify":"0","strLenderName":"0","strBorrowConsultNo":"","lBorrowerBillId":200089155,"strPhoneNO":"DHhMsINbOGyXLAvVh1bi2g==","tsRefreshTime":"2019-04-10 00:01:01","strProductName":"0","nFundYearRate":0,"lCompensatoryPrincipal":16674,"strUnderName":"0","lAmount":200000,"nTermIndex":12,"strLoanDate":"2018-04-09 13:20:49","strBorrowerName":"谭淇","lId":74083,"nTermCount":12,"lCompensatoryAmount":20094,"dtCreateTime":"2019-04-10 00:01:01","strUnderIdentify":"0","strRepayDate":"2019-04-09","lPartnerInterest":3270,"strIncomeTransferDealSN":"","lUnderId":0,"copayState":0,"strBorrowEndDate":"2019-04-09","strReserved":"<?xml version=\"1.0\" encoding=\"UTF-8\"?><CDO><LF N=\"lCompensatoryTaskId\" V=\"4762641\"/><LF N=\"lBorrowIntentId\" V=\"200026131\"/><LF N=\"lBorrowerId\" V=\"24225\"/><LF N=\"nBorrowMode\" V=\"85\"/><LF N=\"lBorrowerBillId\" V=\"200089155\"/></CDO>","lLenderId":0},"orgCode":"DF","productId":"A0001","requestId":"ec75bd009c744fd1b3890b432925d20b","requestTime":"2019-04-10 00:03:42"}

发现上面有明显的差异,在网上进行相关搜索,最后发现是由于转换过程中,新版的fastjson对json进行了重新排序导致的,下面进行修改,代码如下:

public class JsonTest {
    public static void main(String[] args) {
        String strRequestDatagram = "{\"bizContent\":{\"nCallbackState\":0,\"nIncomeTransferState\":0,\"lCompensatoryInterest\":3420,\"nBorrowMode\":85,\"lCompensatoryPenalty\":0,\"nInterestDays\":0,\"lProductId\":0,\"nBorrowerMonthRate\":0,\"lBorrowerId\":24225,\"lBorrowIntentId\":200026131,\"strBorrowerIdentify\":\"430721199512257311\",\"nDebtApplyState\":0,\"strLenderIdentify\":\"0\",\"strLenderName\":\"0\",\"strBorrowConsultNo\":\"\",\"lBorrowerBillId\":200089155,\"strPhoneNO\":\"DHhMsINbOGyXLAvVh1bi2g==\",\"tsRefreshTime\":\"2019-04-10 00:01:01\",\"strProductName\":\"0\",\"nFundYearRate\":0,\"lCompensatoryPrincipal\":16674,\"strUnderName\":\"0\",\"lAmount\":200000,\"nTermIndex\":12,\"strLoanDate\":\"2018-04-09 13:20:49\",\"lId\":74083,\"strBorrowerName\":\"谭淇\",\"nTermCount\":12,\"lCompensatoryAmount\":20094,\"dtCreateTime\":\"2019-04-10 00:01:01\",\"strUnderIdentify\":\"0\",\"lPartnerInterest\":3270,\"strRepayDate\":\"2019-04-09\",\"strIncomeTransferDealSN\":\"\",\"copayState\":0,\"lUnderId\":0,\"strBorrowEndDate\":\"2019-04-09\",\"strReserved\":\"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><CDO><LF N=\\\"lCompensatoryTaskId\\\" V=\\\"4762641\\\"/><LF N=\\\"lBorrowIntentId\\\" V=\\\"200026131\\\"/><LF N=\\\"lBorrowerId\\\" V=\\\"24225\\\"/><LF N=\\\"nBorrowMode\\\" V=\\\"85\\\"/><LF N=\\\"lBorrowerBillId\\\" V=\\\"200089155\\\"/></CDO>\",\"lLenderId\":0},\"orgCode\":\"DF\",\"productId\":\"A0001\",\"requestId\":\"ec75bd009c744fd1b3890b432925d20b\",\"requestTime\":\"2019-04-10 00:03:42\"}";
        System.out.println(strRequestDatagram);

        TreeMap requestDatagram = JSON.parseObject(strRequestDatagram, TreeMap.class, Feature.OrderedField);
        System.out.println(JSON.toJSONString(requestDatagram));
    }
}

返回结果如下:

{"bizContent":{"nCallbackState":0,"nIncomeTransferState":0,"lCompensatoryInterest":3420,"nBorrowMode":85,"lCompensatoryPenalty":0,"nInterestDays":0,"lProductId":0,"nBorrowerMonthRate":0,"lBorrowerId":24225,"lBorrowIntentId":200026131,"strBorrowerIdentify":"430721199512257311","nDebtApplyState":0,"strLenderIdentify":"0","strLenderName":"0","strBorrowConsultNo":"","lBorrowerBillId":200089155,"strPhoneNO":"DHhMsINbOGyXLAvVh1bi2g==","tsRefreshTime":"2019-04-10 00:01:01","strProductName":"0","nFundYearRate":0,"lCompensatoryPrincipal":16674,"strUnderName":"0","lAmount":200000,"nTermIndex":12,"strLoanDate":"2018-04-09 13:20:49","lId":74083,"strBorrowerName":"谭淇","nTermCount":12,"lCompensatoryAmount":20094,"dtCreateTime":"2019-04-10 00:01:01","strUnderIdentify":"0","lPartnerInterest":3270,"strRepayDate":"2019-04-09","strIncomeTransferDealSN":"","copayState":0,"lUnderId":0,"strBorrowEndDate":"2019-04-09","strReserved":"<?xml version=\"1.0\" encoding=\"UTF-8\"?><CDO><LF N=\"lCompensatoryTaskId\" V=\"4762641\"/><LF N=\"lBorrowIntentId\" V=\"200026131\"/><LF N=\"lBorrowerId\" V=\"24225\"/><LF N=\"nBorrowMode\" V=\"85\"/><LF N=\"lBorrowerBillId\" V=\"200089155\"/></CDO>","lLenderId":0},"orgCode":"DF","productId":"A0001","requestId":"ec75bd009c744fd1b3890b432925d20b","requestTime":"2019-04-10 00:03:42"}
{"bizContent":{"nCallbackState":0,"nIncomeTransferState":0,"lCompensatoryInterest":3420,"nBorrowMode":85,"lCompensatoryPenalty":0,"nInterestDays":0,"lProductId":0,"nBorrowerMonthRate":0,"lBorrowerId":24225,"lBorrowIntentId":200026131,"strBorrowerIdentify":"430721199512257311","nDebtApplyState":0,"strLenderIdentify":"0","strLenderName":"0","strBorrowConsultNo":"","lBorrowerBillId":200089155,"strPhoneNO":"DHhMsINbOGyXLAvVh1bi2g==","tsRefreshTime":"2019-04-10 00:01:01","strProductName":"0","nFundYearRate":0,"lCompensatoryPrincipal":16674,"strUnderName":"0","lAmount":200000,"nTermIndex":12,"strLoanDate":"2018-04-09 13:20:49","lId":74083,"strBorrowerName":"谭淇","nTermCount":12,"lCompensatoryAmount":20094,"dtCreateTime":"2019-04-10 00:01:01","strUnderIdentify":"0","lPartnerInterest":3270,"strRepayDate":"2019-04-09","strIncomeTransferDealSN":"","copayState":0,"lUnderId":0,"strBorrowEndDate":"2019-04-09","strReserved":"<?xml version=\"1.0\" encoding=\"UTF-8\"?><CDO><LF N=\"lCompensatoryTaskId\" V=\"4762641\"/><LF N=\"lBorrowIntentId\" V=\"200026131\"/><LF N=\"lBorrowerId\" V=\"24225\"/><LF N=\"nBorrowMode\" V=\"85\"/><LF N=\"lBorrowerBillId\" V=\"200089155\"/></CDO>","lLenderId":0},"orgCode":"DF","productId":"A0001","requestId":"ec75bd009c744fd1b3890b432925d20b","requestTime":"2019-04-10 00:03:42"}

TreeMap解析添加 Feature.OrderedField 返回结果一致,ok

在解决问题的过程中,也看到了很多其他的坑,例如

1.2.3之后的版本,Map的序列化没有做排序再输出,原因是通过TreeMap排序很影响性能。1.2.27版本中增加SerializerFeature.MapSortField实现同样的功能。 使用方法如下: a) 传入SerializerFeature.MapSortField参数。 JSON.toJSONString(map, SerializerFeature.MapSortField); b) 通过代码修改全局缺省配置。 JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.MapSortField.getMask(); c) 通过JVM启动参数配置修改全局配置 -Dfastjson.serializerFeatures.MapSortField=true d) 通过类路径下的fastjson.properties来配置 fastjson.serializerFeatures.MapSortField=true

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值