当前位置:网站首页>@JsonSerialize注解的使用
@JsonSerialize注解的使用
2022-07-29 07:40:00 【shstart7】
使用场景
在开发中,我们将对象序列化为JSON传输给前端,有时候我们的某个或者某些字段需要特殊处理,比如我们有一个日期字段,我们希望当日期为NULL时给前端不返回NULL而返回为未完成等信息,或者我们有一些状态字段,我们需要给前端返回状态对应的信息,此时我们就需要自定义字段的序列化。这就是@JsonSerialize的用处
nullsUsing用法
假设我们的项目中有一个Date类型的字段,我们的需求是当此字段为NULL时,我们在序列化数据的时候序列化成 "未完成"(前端也可以做,假设前端比较懒hhh)
@ApiModelProperty("完成时间")
private Date finishTime;
第一步,我们先写一个指定的处理类
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.util.Date;
//继承JsonSerializer
public class DateJsonSerialize extends JsonSerializer<Date> {
//重写serialize方法
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
//自定义处理方式
jsonGenerator.writeString("未完成");
}
}
第二步,在此字段上加上@JsonSerialize注解,并且使用nullsUsing属性,即当此字段为NULL时就使用我们自定义的解析类序列化此字段(注意当此字段不为NULL时不执行此配置)
@ApiModelProperty("完成时间")
@JsonSerialize(nullsUsing = DateJsonSerialize.class)
private Date finishTime;
测试
将finishTime设置为NULL,然后进行测试查询,序列化为了未完成

using用法
当字段不为NULL时的自定义序列化方式,用法跟上面一样
@ApiModelProperty("完成时间")
@JsonSerialize(using = XXX.class) //自定义序列化类
private Date finishTime;
边栏推荐
- ef core 读取text类型慢_ef core读取大字符串字段慢
- How can electronic component trading enterprises solve warehouse management problems with ERP system?
- Use of gcc/g++
- do end用法的妙处
- Leetcode buckle classic problem -- 4. Find the median of two positively ordered arrays
- NLP introduction + practice: Chapter 5: using the API in pytorch to realize linear regression
- [summer daily question] Luogu p7760 [coci2016-2017 5] tuna
- 小D的刺绣
- 【MYSQL】-【子查询】
- How does MySQL convert rows to columns?
猜你喜欢

监听页面滚动位置定位底部按钮(包含页面初始化定位不对鼠标滑动生效的解决方案)

零数科技深度参与信通院隐私计算金融场景标准制定
![[summer daily question] Luogu p6461 [coci2006-2007 5] trik](/img/bf/c0e03f1bf477730f0b3661b3256d1d.png)
[summer daily question] Luogu p6461 [coci2006-2007 5] trik

How does MySQL convert rows to columns?

IonIcons图标大全

Use of gcc/g++

Leetcode buckle classic problem -- 4. Find the median of two positively ordered arrays

【MYSQL】-【子查询】

美智光电IPO被终止:年营收9.26亿 何享健为实控人

STM32 operation w25q256 w25q16 SPI flash
随机推荐
MySQL 45 讲 | 07 行锁功过:怎么减少行锁对性能的影响?
The new generation of public chain attacks the "Impossible Triangle"
logback appender简介说明
IonIcons图标大全
Starting process of raspberry pie
【深度学习】数据准备-pytorch自定义图像分割类数据集加载
Scala higher order (10): exception handling in Scala
【暑期每日一题】洛谷 P6500 [COCI2010-2011#3] ZBROJ
[WPF] realize language switching through dynamic / static resources
What is the function of fileappender in logback?
The difference between static library and dynamic library of program
强连通分量
Log4qt memory leak, use of heob memory detection tool
美智光电IPO被终止:年营收9.26亿 何享健为实控人
Android interview question | how to write a good and fast log library?
jdbc入门
Introduction to logback appender
Write some DP
[summer daily question] Luogu p7760 [coci2016-2017 5] tuna
MySQL 45讲 | 08 事务到底是隔离的还是不隔离的?