当前位置:网站首页>@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;
边栏推荐
- Output 1234 three digits without repetition
- Sqlmap (SQL injection automation tool)
- The beauty of do end usage
- Access数据库引入datagridview数据后,显示错误
- 功能自动化测试实施的原则以及方法有哪些?
- How can electronic component trading enterprises solve warehouse management problems with ERP system?
- NFT 的 10 种实际用途
- Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
- 09 bloom filter
- Multi thread shopping
猜你喜欢

PAT甲级 1146 拓扑顺序
![[summer daily question] Luogu p6461 [coci2006-2007 5] trik](/img/bf/c0e03f1bf477730f0b3661b3256d1d.png)
[summer daily question] Luogu p6461 [coci2006-2007 5] trik

2022 Shenzhen Cup Title A: get rid of "scream effect" and "echo room effect" and get out of the "information cocoon room"

cs61abc分享会(六)程序的输入输出详解 - 标准输入输出,文件,设备,EOF,命令行参数

MySQL 45讲 | 08 事务到底是隔离的还是不隔离的?

【无标题】格式保存

QT连接两个qslite数据库报错QSqlQuery::exec: database not open

How to establish EDI connection with Scania in Scania?

Log4qt memory leak, use of heob memory detection tool

Leetcode buckle classic problem -- 4. Find the median of two positively ordered arrays
随机推荐
LANDSCAPE
Female graduate students do "mind mapping" and quarrel with their boyfriend! Netizen: the "king of infighting" in the quarrel
Sort out the two NFT pricing paradigms and four solutions on the market
UPC 小C的王者峡谷
3-global exception handling
Prepare esp32 environment
[summer daily question] Luogu p6461 [coci2006-2007 5] trik
Introduction to logback appender
【MYSQL】-【子查询】
Better performance and simpler lazy loading of intersectionobserverentry (observer)
207.课程表
Strongly connected component
09 bloom filter
MySQL uses date_ FORMAT(date,'%Y-%m')
@RequestMapping 用法详解
207. Curriculum
【无标题】格式保存
2022年深圳杯A题破除“尖叫效应”与“回声室效应”走出“信息茧房”
jdbc入门
Introduction to log4j layout