大本赢彩票开户

码迷,xscp18.com
大本赢彩票开户 > 其他好文 > 详细

Jackson中处理map中的null key 或者null value 及实体字段中的null value

时间:2019-09-01 01:45:37      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:end   oba   user   finish   exit   private   tutorials   gets   xtend   

1.map中有null key时的序列化

 当有null key时,jackson序列化会报 Null key for a Map not allowed in JSON (use a converting NullKeySerializer?) 

处理此异常有两种方式

  • 1.需要自定义一个序列化null key的方法
  • 2. map中直接remove null key

 

这里只讨论第一种:

处理方法为 mapper。getSerializerProvider()。setNullKeySerializer(new NullKeySerializer()); 将null key处理为空字符

    @Test
    public void testSerializMapNullKey() throws JsonProcessingException {
        //ignore map null value
        Map<String, Object> map = new HashMap<>();
        map。put(null, "test");
        ObjectMapper mapper = new ObjectMapper();
        mapper.getSerializerProvider().setNullKeySerializer(new NullKeySerializer());
        System。out。println(mapper。writeValueAsString(map));

    }


static class NullKeySerializer extends StdSerializer<Object> {
        public NullKeySerializer() {
            this(null);
        }

        public NullKeySerializer(Class<Object> t) {
            super(t);
        }

        @Override
        public void serialize(Object nullKey, JsonGenerator jsonGenerator, SerializerProvider unused)
                throws IOException, JsonProcessingException {
            jsonGenerator。writeFieldName("");
        }
    }
{"":"test"}

Process finished with exit code 0

2. 处理null value的情况

    @Test
    public void testIgnoreMapNullValue() throws JsonProcessingException {
        //ignore null key
        Map<String, Object> map = new HashMap<>();
        map。put("key", "test");
        map。put("key1", null);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        System.out。println(mapper。writeValueAsString(map));

    }

大本赢彩票开户输出为:{"key":"test"} 并没有 key1:null

{"key":"test"}

Process finished with exit code 0

3. 处理实体中filed中值为null的情况

   使用注解:@JsonInclude(JsonInclude.Include.NON_NULL)

   注意:@JsonInclude(JsonInclude。Include。NON_NULL)即可以在实体上用,也可以在filed中使用,比如在name上用这个注解

 

    @Test
    public void testIgnoreNullFiled() throws JsonProcessingException {
        //test ignore null filed
        User user = new User();
        user。setName(null);
        user。setHouse("asdf");
        ObjectMapper mapper = new ObjectMapper();
        System。out.println(mapper.writeValueAsString(user));

    }

    /**
     * ignore null filed
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    class User {
        private String name;
        private String house;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getHouse() {
            return house;
        }

        public void setHouse(String house) {
            this.house = house;
        }
    }

输出为:

{"house":"asdf"}

Process finished with exit code 0

 

如果设置全局怎么设置呢,使用: mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

    @Test
    public void testIgnoreNullFiledGlobally() throws JsonProcessingException {
        //test ignore null filed
        User user = new User();
        user。setName(null);
        user.setHouse("asdf");
        ObjectMapper mapper = new ObjectMapper();

        //Ignore Null Fields Globally
        mapper。setSerializationInclusion(JsonInclude。Include。NON_NULL);
        System。out.println(mapper.writeValueAsString(user));

    }

 

参考:1.https://www.baeldung.com/jackson-map-null-values-or-null-key

         2.https://github.com/eugenp/tutorials/tree/master/jackson-simple

 

大本赢彩票开户Jackson中处理map中的null key 或者null value 及实体字段中的null value

标签:end   oba   user   finish   exit   private   tutorials   gets   xtend   

原文地址:https://www.cnblogs.com/guanbin-529/p/11441061.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
           
© 2014 xscp18.com 版权所有 京ICP备13008772号-2
迷上了代码!
鼎盛彩票开户 菜鸟彩票开户 天易彩票开户 英豪彩票开户 万发彩票开户 盛通彩票开户 财神彩票开户 向日葵彩票开户 大丰彩票开户 菠萝彩票开户