在项目中有事需要对值为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 -序列化-详解的更多相关文章

  1. Springboot mini - Solon详解(二)- Solon的核心

    Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...

  2. SpringBoot之DispatcherServlet详解及源码解析

    在使用SpringBoot之后,我们表面上已经无法直接看到DispatcherServlet的使用了.本篇文章,带大家从最初DispatcherServlet的使用开始到SpringBoot源码中Di ...

  3. SpringBoot Profile使用详解及配置源码解析

    在实践的过程中我们经常会遇到不同的环境需要不同配置文件的情况,如果每换一个环境重新修改配置文件或重新打包一次会比较麻烦,Spring Boot为此提供了Profile配置来解决此问题. Profile ...

  4. Spring全家桶——SpringBoot之AOP详解

    Spring全家桶--SpringBoot之AOP详解 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关键单元是类,而在AOP中,模块化单元是方 ...

  5. Springboot mini - Solon详解(四)- Solon的事务传播机制

    Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...

  6. Springboot mini - Solon详解(三)- Solon的web开发

    Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...

  7. Springboot mini - Solon详解(五)- Solon扩展机制之Solon Plugin

    Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...

  8. Springboot mini - Solon详解(六)- Solon的校验框架使用、定制与扩展

    Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...

  9. Springboot mini - Solon详解(七)- Solon Ioc 的注解对比Spring及JSR330

    Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...

随机推荐

  1. Linux通过端口号查看使用进程-结束进程

    1. 查看进程(参数带 - 与不带有区别): command [options] 例:ps  -a(配合其他options参数以展示进程更多参数) ps -ef | grep 进程名(返回值是该进程的 ...

  2. go 学习笔记之环境搭建

    千里之行始于足下,开始 Go 语言学习之旅前,首先要搭建好本地开发环境,然后就可以放心大胆瞎折腾了. Go 的环境安装和其他语言安装没什么特别注意之处,下载安装包下一步下一步直到完成,可能唯一需要注意 ...

  3. json数据转换成结构体

    package main import ( "encoding/json" "fmt" ) type IT1 struct { Company string ` ...

  4. CF-weekly4 F. Kyoya and Colored Balls

    https://codeforces.com/gym/253910/problem/F F. Kyoya and Colored Balls time limit per test 2 seconds ...

  5. c语言新知

    C语言学得有点懈怠,昨个遇一个高手. 教了我两天,真细腻. 第一天,让我写一个程序删除字符串多余空格. 我俩代码对比   结果很显然了.

  6. Win10安装 oracle11g 出现INS-13001环境不满足最低要求解决方法

    Win10安装 oracle11g 出现INS-13001环境不满足最低要求 首先,打开你的解压后的database文件夹,找到stage,然后cvu,找到cvu_prereq.xml文件,用note ...

  7. gradle是什么

    Gradle是JVM生态下又一款开源的自动化构建工具,以基于Groovy的DSL描述构建过程,在结合了ant和maven的优点的基础上,提供了更为简洁,灵活和高效的build体验. Task代表了构建 ...

  8. 公式推导【IoUNet//ECCV2018】

    Jiang B, Luo R, Mao J, Xiao T, Jiang Y. Acquisition of localization confidence for accurate object d ...

  9. centos上tcp抓包

    tcpdump host 10.1.131.75 -i eth0  -w data.cap 其中,10.1.131.75上目标机器的IP,eth0上网卡名称,data.cap上抓包数据写入的文件.

  10. 在项目中使用Solr

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...