分享知识 传递快乐

在使用 fastjson 打印请求报文时,突然出现提示错误:

write javaBean error, fastjson version 1.2.71, class xxx...., fieldName : 0。

问题分析:

请求报文:

[{
"receiptAmount": 0.0,
"poStatus": 2,
"customItem204__c": 1,
"po": "O20211118-0020",
"operate": "update",
"paymentPercent": 0.0,
"balance": 0.0,
"customItem214__c": 1,
"customItem226__c": "[{\"fileId\":2006083573301958,\"fileName\":\"56829a66-5b4e-40a8-a4b7-4bf47a6f1acc.png\",\"name\":\"56829a66-5b4e-40a8-a4b7-4bf47a6f1acc.png\",\"fileLength\":5182,\"size\":5182,\"fileUrl\":\"https://crmtencentprod-1253467224.cos.ap-guangzhou.myqcloud.com/636738865791652/2021/11/18/8c77b789-1a32-4ce5-8482-fc69663fcff8.png\",\"url\":\"https://crmtencentprod-1253467224.cos.ap-guangzhou.myqcloud.com/636738865791652/2021/11/18/8c77b789-1a32-4ce5-8482-fc69663fcff8.png\",\"filePixels\":{\"small\":{\"width\":258,\"height\":206},\"large\":{\"width\":258,\"height\":206},\"origin\":{\"width\":258,\"height\":206}},\"pic\":\"258*206\",\"lpic\":\"258*206\",\"fileLUrl\":\"https://crmtencentprod-1253467224.cos.ap-guangzhou.myqcloud.com/636738865791652/2021/11/18/l_8c77b789-1a32-4ce5-8482-fc69663fcff8.png\",\"lurl\":\"https://crmtencentprod-1253467224.cos.ap-guangzhou.myqcloud.com/636738865791652/2021/11/18/l_8c77b789-1a32-4ce5-8482-fc69663fcff8.png\",\"fileSUrl\":\"https://crmtencentprod-1253467224.cos.ap-guangzhou.myqcloud.com/636738865791652/2021/11/18/s_8c77b789-1a32-4ce5-8482-fc69663fcff8.png\",\"surl\":\"https://crmtencentprod-1253467224.cos.ap-guangzhou.myqcloud.com/636738865791652/2021/11/18/s_8c77b789-1a32-4ce5-8482-fc69663fcff8.png\",\"type\":1}]",
"payments": 0.0,
"lockStatus": 1,
"updatedBy": 1839350438101707,
"customItem166__c": 1658305003061909,
"applicantId": 1839350438101707,
"customItem174__c": 0.0,
"overdueStatus": 2,
"initAmount": 0.0,
"approvalStatus": 3,
"actualInvoicedAmount": 0.0,
"comment": "111",
"contractId": 1731633919640292,
"dimDepart": 655543879385770,
"receiptBalance": 0.0,
"customItem196__c": 0.0,
"customItem209__c": 2,
"surplusPayable": 0.0,
"priceId": 640151491068629,
"updatedAt": 1637207142877,
"customItem220__c": 0.0,
"totalAdditionalDiscountAmount": 0.0,
"apiKey": "order",
"accountId": 753630970987206,
"customItem225__c": 2,
"customItem173__c": 1.0,
"customItem219__c": 5,
"paymentStatus": 1,
"totalDiscountAmount": 0.0,
"customItem206__c": 2,
"lineItemCount": 1,
"ownerId": 1839350438101707,
"id": 2006083639362213,
"roStatus": 1,
"customerAccount__c": "100016961262",
"customItem222__c": 0.0,
"listTotal": 0.0,
"amountUnbilled": 0.0,
"totalPaymentAmount": 0.0,
"effectiveDate": 1637206472634,
"entityType": 636789772255911,
"belongId": 35,
"totalInvoiceAdjustmentAmount": 0.0,
"customItem216__c": "十大科技是",
"customItem190__c": 1,
"customItem194__c": 1,
"paidAmount": 0.0,
"totalRefundAmount": 0.0,
"transactionDate": 1637206320000,
"createdAt": 1637206440128,
"customItem228__c": 2,
"orderVersion": 1,
"amountInvoiced": 0.0,
"productsAmount": 1.0,
"customItem191__c": 1,
"totalSystemDiscountAmount": 0.0,
"invoiceBalance": 0.0,
"createdBy": 1839350438101707,
"amount": 0.0,
"paymentBalance": 0.0,
"customItem229__c": "AC-YW-20210508-0001",
"customItem192__c": 1,
"paymentAmountPlanned": 0.0,
"customItem215__c": 2
}]

经排查发现,报错的原因是因为报文中的 customItem226__c 字段的内容在 fastjson 序列化对象时报错导致。

解决序列化为题如下:

1、在字段上添加注解 @JSONField(serialize =false), 这样做不会报错可以顺利打印,但是添加该注解的字段将不会被序列化并放入到json字符串中

2、在 toJSONString 调用过程中加入 SerializerFeature.IgnoreErrorGetter (推荐)

例如:JSON.toJSONString(requestErrorInfo, SerializerFeature.IgnoreErrorGetter))

—————————
如有不足请留言指正
相互学习,共同进步