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. vue是如何通过diff算法做渲染更新

    渲染页面 图中框起来的部分,vue会根据响应式变化的通知生成一颗新的 Virtual Dom Tree,然后将新的Virtual Dom Tree 和之前的 Virtual Dom Tree 做 di ...

  2. .net core中Grpc使用报错:Request protocol 'HTTP/1.1' is not supported.

    显然这个报错是说HTTP/1.1不支持. 首先,我们要知道,Grpc是Google开源的,跨语言的,高性能的远程过程调用框架,它是以HTTP/2作为通信协议的,所以当我启动启用一个服务作为Grpc的服 ...

  3. Pycharm_关闭PEP8函数名不能包含大写字母的规范

    屏蔽PEP8告警 全是小写字母,可能与以往的习惯不大一样,将这样的警告忽略的方法如下: File →Settings→Editor→Inspections→Python→PEP 8 naming co ...

  4. vuex从后台数据后页面已完成渲染无法显示数据的解决办法

    一.在store中state定义一个变量 来控制是否显示 二.在完成数据获取后把isShow设为true 三.把state状态映射到页面的computed中 四.在模板中使用v-if来判断是否显示 来 ...

  5. SYCOJ1793

    题目-统计单词前缀数 (shiyancang.cn) 1 #include<bits/stdc++.h> 2 using namespace std; 3 map<string,in ...

  6. Centos下安装Maven私服Nexus

    dockers安装Nexus,指定访问路径(默认为/:在使用Nginx做反向代理时,最好指定访问路径),并在容器外持久化数据,避免Nexus容器升级后数据丢失. 安装并启动 docker run -d ...

  7. Java反射给泛型集合赋值

    Java反射给泛型集合赋值 泛型 Java泛型简单描述下: 比如创建一个List集合,我想在里边只放Student对象信息,就需要写成 List<Student> studentList ...

  8. Cesium参考资源

    Reference resources cesium官网 cesium 下载 cesium官方文档 APIs cesium-workshop github cesium 官方示例 cesium git ...

  9. pytest文档5-参数化parametrize

    pytest.mark.parametrize装饰器可以实现测试用例参数化. parametrizing 1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子 # content of tes ...

  10. Spring Boot Starter 和 ABP Module

    Spring Boot 和 ABP 都是模块化的系统,分别是Java 和.NET 可以对比的框架.模块系统是就像乐高玩具一样,一块一块零散积木堆积起一个精彩的世界.每种积木的形状各不相同,功能各不相同 ...