当前位置:网站首页>@Use of jsonserialize annotation
@Use of jsonserialize annotation
2022-07-29 07:42:00 【shstart7】
Use scenarios
In development , We serialize the object as JSON To the front end , Sometimes our Some or some fields Require special treatment , For example, we have a date field , We hope that the date will be NULL Do not return to the front end NULL And return as Hang in the air Etc , Or we have some status fields , We need to return the information corresponding to the status to the front end , At this point, we need to customize the serialization of fields . This is it. @JsonSerialize Usefulness
nullsUsing usage
Suppose there is one in our project Date Type field , Our requirement is when this field is NULL when , When serializing data, we serialize it into " Hang in the air "( The front end can also be used , Suppose the front end is lazy hhh)
@ApiModelProperty(" Completion time ")
private Date finishTime;
First step , Let's first write a specified processing class
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;
// Inherit JsonSerializer
public class DateJsonSerialize extends JsonSerializer<Date> {
// rewrite serialize Method
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
// Custom handling
jsonGenerator.writeString(" Hang in the air ");
}
}
The second step , Add @JsonSerialize annotation , And use nullsUsing attribute , That is, when this field is NULL Use our custom parsing class to serialize this field ( Note that when this field is not NULL Do not execute this configuration )
@ApiModelProperty(" Completion time ")
@JsonSerialize(nullsUsing = DateJsonSerialize.class)
private Date finishTime;
test
take finishTime Set to NULL, Then test the query , Serialize as Hang in the air

using usage
When the field is not NULL Custom serialization method when , The usage is the same as above
@ApiModelProperty(" Completion time ")
@JsonSerialize(using = XXX.class) // Custom serialization class
private Date finishTime;
边栏推荐
- State machine DP 3D
- Sqlmap(SQL注入自动化工具)
- 受欢迎的牛 G
- 零数科技深度参与信通院隐私计算金融场景标准制定
- 【暑期每日一题】洛谷 P6408 [COCI2008-2009#3] PET
- How can electronic component trading enterprises solve warehouse management problems with ERP system?
- The new generation of public chain attacks the "Impossible Triangle"
- [untitled] format save
- IonIcons图标大全
- Prometheus and grafana
猜你喜欢

Use custom annotations to verify the size of the list
![[WPF] realize language switching through dynamic / static resources](/img/23/1e089ce4a07128323824b25897a8c4.png)
[WPF] realize language switching through dynamic / static resources

The new generation of public chain attacks the "Impossible Triangle"

207.课程表

Halcon installation and testing in vs2017, DLL configuration in vs2017
![【暑期每日一题】洛谷 P6461 [COCI2006-2007#5] TRIK](/img/bf/c0e03f1bf477730f0b3661b3256d1d.png)
【暑期每日一题】洛谷 P6461 [COCI2006-2007#5] TRIK

新生代公链再攻「不可能三角」
![[summer daily question] Luogu p6461 [coci2006-2007 5] trik](/img/bf/c0e03f1bf477730f0b3661b3256d1d.png)
[summer daily question] Luogu p6461 [coci2006-2007 5] trik

关于大龄读博的几点回答?

RoBERTa:A Robustly Optimized BERT Pretraining Approach
随机推荐
MySQL 45讲 | 08 事务到底是隔离的还是不隔离的?
状态机dp三维
Halcon installation and testing in vs2017, DLL configuration in vs2017
Pat class a 1154 vertex shading
You study, I reward, 21 day learning challenge | waiting for you to fight
For the application challenge of smart city, shengteng AI gives a new solution
多线程购物
207. Curriculum
NLP introduction + practice: Chapter 5: using the API in pytorch to realize linear regression
你学习·我奖励,21天学习挑战赛 | 等你来战
Output 1234 three digits without repetition
Gin abort cannot prevent subsequent code problems
零数科技深度参与信通院隐私计算金融场景标准制定
do end用法的妙处
[summer daily question] Luogu p6320 [coci2006-2007 4] sibice
zip gzip tar压缩进阶版
Prepare esp32 environment
MapReduce各阶段步骤
Calculate program run time demo
cs61abc分享会(六)程序的输入输出详解 - 标准输入输出,文件,设备,EOF,命令行参数