Jackson中处理map中的null key 或者null value 及实体字段中的null value
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的更多相关文章
- 在论坛中出现的比较难的sql问题:16(取一个字段中的数字)
原文:在论坛中出现的比较难的sql问题:16(取一个字段中的数字) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路. 问题:取一个字段中的数字http://bbs.csdn ...
- foreach属性-动态-mybatis中使用map类型参数,其中key为列名,value为列值
http://zhangxiong0301.iteye.com/blog/2242723 最近有个需求,就是使用mybatis时,向mysql中插入数据,其参数为map类型,map里面的key为列名, ...
- 向数据库中插入一个DateTime类型的数据到一个Date类型的字段中,需要转换类型。TO_DATE('{0}','YYYY-MM-DD'))
需要指出的是,C#中有datetime类型,但是这个类型是包括小时,分钟,秒的.这个格式与数据库中的Date类型不符,如果将now设为datetime类型插入数据会失败. 需要通过TO_DATE('字 ...
- mysql 允许在唯一索引的字段中出现多个null值
线上问题:org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [update fl_table ...
- 程序处理数据库中值字段值为null的查询显示
1.如果你做了一个简单的注册界面,需要用户进行注册,但有些项是不必要填的,当用户完成注册时,数据库表中的相应字段的值会写入null,但如何将查询的字段的值null显示出来? 2.首先我们学习一下如何向 ...
- mysql 中查询一个字段是否为null的sql
查询mysql数据库表中字段为null的记录: select * 表名 where 字段名 is null 查询mysql数据库表中字段不为null的记录: select * 表名 where 字段名 ...
- 当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出?
当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出? 问题: orderStatus 和 payStatus都是枚举类,并且枚举的个数达地10来个,我们不可能在模板页面(jsp/ftl ...
- Java中Map根据键值(key)或者值(value)进行排序实现
我们都知道,java中的Map结构是key->value键值对存储的,而且根据Map的特性,同一个Map中 不存在两个Key相同的元素,而value不存在这个限制.换句话说,在同一个Map中Ke ...
- jstl中取map,其中map的key是一个对象,value是一个list
<c:forEach items="${map }" var="item"> //取得key中的属性 ${item.key.name } <c ...
随机推荐
- Capstone CS5213|HDMI转VGA|CS5213设计参考电路
Capstone CS5213是一款HDMI到VGA转换器结合了HDMI输入接口和模拟RGB DAC输出且带支持片上音频数模转换器.CS5213芯片设计简单,整体芯片尺寸精悍,外围电路集成优化度较高, ...
- python+requests传两种参数体
在JMeter请求参数中,我们了解到,在做接口测试时,发送请求的参数有两种格式,一种是Parameters,一种是JSON.怎么区分请看 https://www.cnblogs.com/testlea ...
- Bash 取字符串的最后 N 个字符 - ${str:0-N:LENGTH}
Bash 取字符串的最后 N 个字符: ${str:0-N:LENGTH} or ${str:0-N} https://tldp.org/LDP/abs/html/string-manipulatio ...
- Eclipse配置Maven3.5
原文: https://www.toutiao.com/i6494558327622599181/ 配置Maven 首先保证Java环境是有的(Maven 3.1以上 要求 JDK 1.6 或以上版本 ...
- Apache Shiro反序列化远程代码执行复现
最近也是看shiro漏洞比较多,所以自己也在本地复现了一下,拿出来与大家一起分享 0x00 关于Apache Shiro Apache shiro是一个Java安全框架,提供了认证.授权.加密和会话管 ...
- 带你自定义实现Spring事件驱动模型
Spring 事件驱动模型概念 Spring 事件驱动模型就是观察者模式很经典的一个应用,我们可以通过Spring 事件驱动模型来完成代码的解耦. 三角色 Spring 事件驱动模型或者说观察者模式需 ...
- 【记录一个问题】用ndk的gcc命令行无法编译C++11的lambda等语法的代码
/Users/ahfu/code/android/android-ndk-r14b/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_6 ...
- 遇到奇怪的问题:web.py 0.40中使用web.input(),出现一堆奇怪的错误
有的请求很正常,有的请求就出现了500错误. 这里使用POST请求,然后在web.input()中出现了很长很长的错误. 猜测是这个机器上安装了python2.7 / python 3.6 / pyt ...
- 带你学习Flood Fill算法与最短路模型
一.Flood Fill(连通块问题) 0.简介 Flood Fill(洪水覆盖) 可以在线性的时间复杂内,找到某个点所在的连通块! 注:基于宽搜的思想,深搜也可以做但可能会爆栈 flood fill ...
- Using Swap
# create swap file dd if=/dev/zero of=/.swap bs=1048576 count=4096 # format swap mkswap /.swap # sta ...