当前位置:网站首页>@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;
边栏推荐
- What are the answers about older bloggers?
- Introduction to logback appender
- Prometheus与Grafana
- 黑盒测试常见错误类型说明及解决方法有哪些?
- QT连接两个qslite数据库报错QSqlQuery::exec: database not open
- [summer daily question] Luogu p6336 [coci2007-2008 2] bijele
- 【WPF】通过动态/静态资源实现语言切换
- 【暑期每日一题】洛谷 P6336 [COCI2007-2008#2] BIJELE
- [summer daily question] Luogu p4414 [coci2006-2007 2] ABC
- 7-2 calculate the area and perimeter of a regular pentagon (25 points)
猜你喜欢

SEGGER 的硬件异常 分析

I, 28, a tester, was ruthlessly dismissed in October: I want to remind people who are still learning to test

电子元器件贸易企业如何借助ERP系统,解决仓库管理难题?

国内数字藏品的乱象与未来

Meizhi optoelectronics' IPO was terminated: annual revenue of 926million he Xiangjian was the actual controller

QT topic: basic components (button class, layout class, output class, input class, container class)

《nlp入门+实战:第五章:使用pytorch中的API实现线性回归》

MySQL 45 | 08 is the transaction isolated or not?

MySQL uses date_ FORMAT(date,'%Y-%m')
功能自动化测试实施的原则以及方法有哪些?
随机推荐
RoBERTa:A Robustly Optimized BERT Pretraining Approach
写点dp
207. Curriculum
[daily question in summer] Luogu p6408 [coci2008-2009 3] pet
性能更佳、使用更简单的懒加载IntersectionObserverEntry(观察者)
halcon的安装以及在vs2017中测试,vs2017中dll的配置
[untitled] format save
For the application challenge of smart city, shengteng AI gives a new solution
Levelfilter introduction
NFT 的 10 种实际用途
Sqlmap (SQL injection automation tool)
NLP introduction + practice: Chapter 5: using the API in pytorch to realize linear regression
I, 28, a tester, was ruthlessly dismissed in October: I want to remind people who are still learning to test
准备esp32环境
小D的刺绣
Cross domain problems when downloading webapi interface files
MapReduce steps of each stage
Can the subset of the array accumulate K
QT连接两个qslite数据库报错QSqlQuery::exec: database not open
The new generation of public chain attacks the "Impossible Triangle"