zoukankan      html  css  js  c++  java
  • 记一次对象序列化不打印value值为null的属性问题

    背景:

      开发时遇到问题,看日志的入参的busiData中没有intRate这个属性,后面转成map时,有个判断

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

    一直报空指针异常,很郁闷这个参数是怎么添加进去的,而且值是null

    测试代码如下:

    @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=}

    结论:

      对象的属性值为null,那么日志中不会打印该属性;属性值为空字符串,日志中会打印该属性

      map中,不论value值为null还是为空字符串,日志中都会打印所有的键值对;

  • 相关阅读:
    Android WebView重定向问题的解决方案
    Android 控件背景选择图片还是drawable XML资源
    Android AlertDialog 绝对位置计算
    Android 5.0以上Material Design 沉浸式状态栏
    Android 6.0系统动态请求系统相机权限
    Android软键盘在清单文件中所有配置含义
    Android,TextView的所有属性和方法
    【转载】Android控件属性大全
    Android布局及控件的属性说明
    android带有文字的图片按钮的两种实现方式
  • 原文地址:https://www.cnblogs.com/pluto-yang/p/9896008.html
Copyright © 2011-2022 走看看