当前位置:网站首页>@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;
边栏推荐
- 输出1234无重复的三位数
- NLP introduction + practice: Chapter 5: using the API in pytorch to realize linear regression
- Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇
- MapReduce steps of each stage
- 08 dynamic programming
- The new generation of public chain attacks the "Impossible Triangle"
- In JS, 0 means false, and non-0 means true
- Can the subset of the array accumulate K
- Meizhi optoelectronics' IPO was terminated: annual revenue of 926million he Xiangjian was the actual controller
- Matlab simulation of LDPC minimum sum decoding based on high-order six ring free
猜你喜欢

Sort out the two NFT pricing paradigms and four solutions on the market

【无标题】格式保存

【深度学习】数据准备-pytorch自定义图像分割类数据集加载

LANDSCAPE

10 practical uses of NFT

梳理市面上的2大NFT定价范式和4种解决方案

Android interview question | how to write a good and fast log library?

Getting started with JDBC
![【暑期每日一题】洛谷 P6461 [COCI2006-2007#5] TRIK](/img/bf/c0e03f1bf477730f0b3661b3256d1d.png)
【暑期每日一题】洛谷 P6461 [COCI2006-2007#5] TRIK
![【暑期每日一题】洛谷 P7760 [COCI2016-2017#5] Tuna](/img/9a/f857538c574fb54bc1accb737d7aec.png)
【暑期每日一题】洛谷 P7760 [COCI2016-2017#5] Tuna
随机推荐
输出1234无重复的三位数
[deep learning] data preparation -pytorch custom image segmentation data set loading
Introduction to log4j layout
The difference between static library and dynamic library of program
do end用法的妙处
Gin abort cannot prevent subsequent code problems
The new generation of public chain attacks the "Impossible Triangle"
[FPGA tutorial case 42] image case 2 - realize image binarization processing through Verilog, and conduct auxiliary verification through MATLAB
What are the principles and methods of implementing functional automation testing?
[summer daily question] Luogu p4413 [coci2006-2007 2] R2
Pat class a 1146 topology sequence
功能自动化测试实施的原则以及方法有哪些?
《nlp入门+实战:第五章:使用pytorch中的API实现线性回归》
UPC little C's King Canyon
log4qt内存泄露问题,heob内存检测工具的使用
BeanUtils.setProperty()
PAT甲级 1150 旅行商问题
【暑期每日一题】洛谷 P6408 [COCI2008-2009#3] PET
新生代公链再攻「不可能三角」
Cs61abc sharing session (VI) detailed explanation of program input and output - standard input and output, file, device, EOF, command line parameters