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\":\"\",\"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":"","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":"","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\":\"\",\"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":"","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":"","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

 

你可能感兴趣的:(java)