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的更多相关文章

  1. 在论坛中出现的比较难的sql问题:16(取一个字段中的数字)

    原文:在论坛中出现的比较难的sql问题:16(取一个字段中的数字) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路. 问题:取一个字段中的数字http://bbs.csdn ...

  2. foreach属性-动态-mybatis中使用map类型参数,其中key为列名,value为列值

    http://zhangxiong0301.iteye.com/blog/2242723 最近有个需求,就是使用mybatis时,向mysql中插入数据,其参数为map类型,map里面的key为列名, ...

  3. 向数据库中插入一个DateTime类型的数据到一个Date类型的字段中,需要转换类型。TO_DATE('{0}','YYYY-MM-DD'))

    需要指出的是,C#中有datetime类型,但是这个类型是包括小时,分钟,秒的.这个格式与数据库中的Date类型不符,如果将now设为datetime类型插入数据会失败. 需要通过TO_DATE('字 ...

  4. mysql 允许在唯一索引的字段中出现多个null值

    线上问题:org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [update fl_table ...

  5. 程序处理数据库中值字段值为null的查询显示

    1.如果你做了一个简单的注册界面,需要用户进行注册,但有些项是不必要填的,当用户完成注册时,数据库表中的相应字段的值会写入null,但如何将查询的字段的值null显示出来? 2.首先我们学习一下如何向 ...

  6. mysql 中查询一个字段是否为null的sql

    查询mysql数据库表中字段为null的记录: select * 表名 where 字段名 is null 查询mysql数据库表中字段不为null的记录: select * 表名 where 字段名 ...

  7. 当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出?

    当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出? 问题: orderStatus 和 payStatus都是枚举类,并且枚举的个数达地10来个,我们不可能在模板页面(jsp/ftl ...

  8. Java中Map根据键值(key)或者值(value)进行排序实现

    我们都知道,java中的Map结构是key->value键值对存储的,而且根据Map的特性,同一个Map中 不存在两个Key相同的元素,而value不存在这个限制.换句话说,在同一个Map中Ke ...

  9. jstl中取map,其中map的key是一个对象,value是一个list

    <c:forEach items="${map }" var="item"> //取得key中的属性 ${item.key.name } <c ...

随机推荐

  1. IT6563替代芯片|DP转HDMI 2.0音视频讯号转换芯片|CS5263Capstpne

    IT6563替代芯片|CS 5263|4LAN DP转HDMI 2.0音视频讯号转换芯片IT6563是一款4LAN EDP转HDMI2.0 HDMI2.0转换器芯片.IT6563FN结合Display ...

  2. 查收新年礼物丨DevEco Studio 3.0 Beta2发布,20个新变化详解

    HUAWEI DevEco Studio是开发HarmonyOS应用和原子化服务的一站式集成开发环境(IDE),为开发者提供工程模板创建.开发.编译.调试.发布等功能. 2021年12月31日,新版本 ...

  3. Kafka集群安装Version2.10

    Kafka集群安装,基于版本2.10, 使用kafka_2.10-0.10.1.0.tgz安装包. 1.安装规划 Storm集群模式,安装到下面三台机器 IP Hostname 10.43.159.2 ...

  4. SQLAlchemy(十)

    ORM操作在实际项目中的应用非常多,涉及到的框架也是根据不同的项目有不同的处理模块,不过操作流程和步骤都是大同小异基本没有什么太大变化,唯一需要注意的就是在实际操作过程中你要使用的ORM框架的处理性能 ...

  5. Word合并多文档

    图片如果损坏,点击链接: https://www.toutiao.com/i6489785099528176142/ 很多时候,我们需要将两个或者多个文档的内容,放到一起,而最直接的办法就是将多个文档 ...

  6. RHCSA 第四天

    1.使用whereis 查找 locate命令 使用which查找whereis命令 使用locate查找rm命令    2.find命令使用: 使用find命令在当前路径下查找所有的普通文件 使用f ...

  7. leetcode 1541. 平衡括号字符串的最少插入次数

    问题描述 给你一个括号字符串 s ,它只包含字符 '(' 和 ')' .一个括号字符串被称为平衡的当它满足: 任何左括号 '(' 必须对应两个连续的右括号 '))' . 左括号 '(' 必须在对应的连 ...

  8. dgv 自动换行

    //设置自动换行 dgv.DefaultCellStyle.WrapMode = DataGridViewTriState.True; //设置自动调整高度 dgv.AutoSizeRowsMode ...

  9. C# 类的继承问题 正方形周长面积计算问题

    1. 设计编写一个控制台应用程序,练习类的继承. (1) 编写一个抽象类 People,具有"姓名","年龄"字段,"姓名"属性,Work ...

  10. 【笔记】thanos ruler组件

    阅读官网文档后的笔记:https://thanos.io/tip/components/rule.md/ 感受 官网第一个话就强调风险,看来坑很多,能不用尽量不用 recording rule &am ...