對象序列化不打印value值爲null的屬性問題

背景:json

  開發時遇到問題,看日誌的入參的busiData中沒有intRate這個屬性,後面轉成map時,有個判斷測試

if(map.containsKey("intRate")){
   BigDecimal amt = new BigDecimal(datas.get("intRate")); 
}

一直報空指針異常,很鬱悶這個參數是怎麼添加進去的,並且值是nullspa

 

測試代碼以下:指針

@Test
    public void test() {
        String json = "{\"busiData\":{\"intRate1\":null,\"intRate2\":\"\",\"overdueFlag\":\"N\",\"firstDue\":\"51.594\",\"loanPeriod\":\"12\"},\"loanAmt\":6000.00,\"loanDays\":30,\"productCode\":\"360JINXIAO\",\"refundMethod\":\"03\"}";
        CouponExchangeInput couponExchangeInput = JSONObject.parseObject(json, CouponExchangeInput.class);
        System.out.println(couponExchangeInput);
        logger.info("couponExchangeInput:{}", couponExchangeInput);
System.out.println(
"---------"); Map<Object, Object> map = new HashMap<>(); map.put("intRate1",null); map.put("intRate2",""); map.put("intRate3","aaa"); System.out.println(map); logger.info("map:{}", map); }

結果:日誌

{"busiData":{"overdueFlag":"N","firstDue":"51.594","loanPeriod":"12","intRate2":""},"loanAmt":6000.00,"loanDays":30,"productCode":"360JINXIAO","refundMethod":"03"}
INFO  [main] [TestCoupon:301] - couponExchangeInput:{"busiData":{"overdueFlag":"N","firstDue":"51.594","loanPeriod":"12","intRate2":""},"loanAmt":6000.00,"loanDays":30,"productCode":"360JINXIAO","refundMethod":"03"}
---------
{intRate3=aaa, intRate1=null, intRate2=}
INFO  [main] [TestCoupon:308] - map:{intRate3=aaa, intRate1=null, intRate2=}

 

結論:code

  對象的屬性值爲null,那麼日誌中不會打印該屬性;屬性值爲空字符串,日誌中會打印該屬性對象

  map中,不論value值爲null仍是爲空字符串,日誌中都會打印全部的鍵值對;blog

相關文章
相關標籤/搜索