Springboot jackSon -序列化-详解
在项目中有事需要对值为NULL的对象中Field不做序列化输入配置方式如下:
[配置类型]:
源码包中的枚举类:
public static enum Include {
ALWAYS,
NON_NULL,
NON_ABSENT,
NON_EMPTY,
NON_DEFAULT,
USE_DEFAULTS; private Include() {
}
}
Include.Include.ALWAYS 默认
Include.NON_DEFAULT 属性为默认值不序列化
Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化
Include.NON_NULL 属性为NULL 不序列化
方式一:全局配置,处理所有整个应用的实体对象
#对日期类型的转换配置
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
# 配置 参数如下 always non_absent non_default non_empty non_null use_defaults
spring.jackson.default-property-inclusion=non_null
方式二:在需要序列的话的实体类上加注解 ->[配置类型]所列
@JsonInclude(Include.NON_NULL)
方式三:配置类型
3.1自定义序列化实现类,可以作用在类上 自定义json序列化需要实现StdSerializer<T>或者JsonSerializer<T>
@JsonSerialize(using = ClientObjectSerialize.class)
public class CreditBorrowerRepaymentRequestDto{
}
实现类:对字段类型转换,以及对值为null字段的过滤
public class ClientObjectSerialize extends JsonSerializer<CreditBorrowerRepaymentRequestDto>{ @Override
public void serialize(CreditBorrowerRepaymentRequestDto dto, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeStartObject();
try {
Field[] fields = dto.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if(null == field.get(dto)){
continue;
}
jsonGenerator.writeFieldName(field.getName());
jsonGenerator.writeObject(field.get(dto));
}
} catch (Exception e) {
e.printStackTrace();
}
jsonGenerator.writeEndObject();
}
}
3.2自定义序列化实现类,可以作用在实体对象字段上,对NULL值的处理,或者转换
@JsonSerialize(using = ClientStringSerialize.class)
private String name; @JsonSerialize(using = ClientDtaeSerialize.class)
private Date date;
public class ClientStringSerialize extends JsonSerializer<String> { @Override
public void serialize(String string, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { if(string == null){
jsonGenerator.writeString(string + "[NULL]");
}else{
jsonGenerator.writeString(string);
}
}
}
public class ClientDtaeSerialize extends JsonSerializer<Date> {
@Override
public void serialize(Date createDate, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(createDate));
}
}
实践总结:
一当全局配置了NULL字段过滤的配置,但有的实体对象需要序列化出NULL的字段值,如何处理?
答: 1. 直接在对象上增加 @JsonInclude(JsonInclude.Include.ALWAYS) 类上的注解优先级比较高,会覆盖全局的配置
2.用自定义的类序列化注解(同上)
二直接在字段上加自定义序列化类会覆盖全局配置吗?
答:不会,有默认的 public class NullSerializer extends StdSerializer<Object> 来处理,当值不为Null的时候才会执行自定义字段上的序列化注解实现类
Springboot jackSon -序列化-详解的更多相关文章
- Springboot mini - Solon详解(二)- Solon的核心
Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...
- SpringBoot之DispatcherServlet详解及源码解析
在使用SpringBoot之后,我们表面上已经无法直接看到DispatcherServlet的使用了.本篇文章,带大家从最初DispatcherServlet的使用开始到SpringBoot源码中Di ...
- SpringBoot Profile使用详解及配置源码解析
在实践的过程中我们经常会遇到不同的环境需要不同配置文件的情况,如果每换一个环境重新修改配置文件或重新打包一次会比较麻烦,Spring Boot为此提供了Profile配置来解决此问题. Profile ...
- Spring全家桶——SpringBoot之AOP详解
Spring全家桶--SpringBoot之AOP详解 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关键单元是类,而在AOP中,模块化单元是方 ...
- Springboot mini - Solon详解(四)- Solon的事务传播机制
Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...
- Springboot mini - Solon详解(三)- Solon的web开发
Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...
- Springboot mini - Solon详解(五)- Solon扩展机制之Solon Plugin
Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...
- Springboot mini - Solon详解(六)- Solon的校验框架使用、定制与扩展
Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...
- Springboot mini - Solon详解(七)- Solon Ioc 的注解对比Spring及JSR330
Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...
随机推荐
- (转)cube-ui后编译
转载地址:https://www.jianshu.com/p/189755f9ce43 1. 后编译介绍 目前大部分的前端项目开发都是使用es6+的代码并且使用babel进行编译,而传统的对代码包的引 ...
- 20191028 牛客网CSP-S Round2019-1
花了 \(30min\) 打了 \(180\) 分的暴力... 仓鼠的石子游戏 问题描述 链接:https://ac.nowcoder.com/acm/contest/1100/A 仓鼠和兔子被禁止玩 ...
- 洛谷P4169 [Violet]天使玩偶/SJY摆棋子
%%%神仙\(SJY\) 题目大意: 一个二维平面,有两种操作: \(1.\)增加一个点\((x,y)\) \(2.\)询问距离\((x,y)\)曼哈顿最近的一个点有多远 \(n,m\le 300 0 ...
- Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 背包dp
D. Yet Another Subarray Problem You are given an array \(a_1, a_2, \dots , a_n\) and two integers \( ...
- 游戏设计艺术 第2版 (Jesse Schell 著)
第1章 太初之时,有设计师 (已看) 第2章 设计师创造体验 第3章 体验发生于场景 第4章 体验从游戏中诞生 第5章 游戏由元素构成 第6章 元素支撑起主题 第7章 游戏始于一个创意 第8章 游戏通 ...
- 记一次SQL调优
insert优化 如果你在某一时刻有大量的insert操作,一条一条插入是非常耗时的.insert语句本身支持一次插入很多条记录,插入记录数上限受sql语句长度限制,一般一次插个几千条是没问题的.在我 ...
- svg描边路径动画
svg描边路径动画<pre><!DOCTYPE html><html> <head> <meta charset="UTF-8" ...
- 三、ForkJoin分析
ForkJoin分析 一.ForkJoin ForkJoin是由JDK1.7后提供多线并发处理框架.ForkJoin的框架的基本思想是分而治之.什么是分而治之?分而治之就是将一个复杂的计算,按照设 ...
- Springboot创建项目(idea版本)
一:概述 由于springboot项目,不管是java工程还是web工程都可以直接以jar方式运行,所以推荐创建jar工程,这里创建jar工程项目为例. 二:两种方式创建springboot项目 1. ...
- VMware 中安装kvm虚拟机
环境准备: 安装vmware时需要自定义安装-开启虚拟化技术 安装成功之后就可以继续进行了. 1 查看CPU是否支持KVM egrep 'vmx|svm' /proc/cpuinfo --colo ...