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. [opencv]使用g++编译opencv程序演示

    gcc/g++ 编译命令 1. gcc -E source_file.c -E,只执行到预编译.直接输出预编译结果. 2. gcc -S source_file.c  -S,只执行到源代码到汇编代码的 ...

  2. Java Web项目 慧心人力资源管理系统

    题目:慧心人力资源管理系统 文档下载:https://download.csdn.net/download/weixin_44893902/16336711 完整项目下载:https://downlo ...

  3. docker学习:docker命令

    帮助命令 自验证 docker version 详情信息 docker info 获取帮助 docker --help 镜像命令 列出本例主机上的镜像 docker images [OPTIONS] ...

  4. Kafka单机安装Version1.0.1(自带Zookeeper)

    1.说明 Kafka单机安装,基于版本1.0.1, 使用kafka_2.12-1.0.1.tgz安装包, 其中2.12是编译工具Scala的版本. 而且不需要另外安装Zookeeper服务, 使用Ka ...

  5. Ranger-Hdfs插件安装

    Ranger-Hdfs插件ranger-0.6.0-hdfs-plugin安装到Hdfs的所有NameNode节点, 其他的DataNode节点不需要安装. 1. 登陆hdfs安装的用户,hdfs/z ...

  6. [特别篇] 记JZ冬令营(Finished)

    1.16 走错班了, 去了全是大佬的1班, 然后灰溜溜滚回2班了. 去参加开营仪式. 然而昏昏欲睡... 实在太累了澡也没洗.. 群英云集, 多是感慨. 当时依依惜别和铮铮誓言, 在重逢中无语凝噎. ...

  7. 2.HTML5基本标签

    一.标题标签  h1-->h6 h1最大 h6最小   <body>   <h1>一级标题</h1>   <h2>二级标题</h2> ...

  8. Sentry 企业级数据安全解决方案 - Relay 项目配置

    内容整理自官方文档 系列 Sentry 企业级数据安全解决方案 - Relay 入门 Sentry 企业级数据安全解决方案 - Relay 运行模式 Sentry 企业级数据安全解决方案 - Rela ...

  9. Java Date 类型比较

    //某时间Date time = tRemind.getTime();//现在时间Date now = new Date();//结果大于0则是现在时间大于某时间//结果等于0则为刚好相等//结果小于 ...

  10. Golang实现集合(set)

    package set package set import ( "bytes" "fmt" "sync" ) type Set struc ...