惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
Google DeepMind News
Google DeepMind News
H
Help Net Security
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
T
Troy Hunt's Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security @ Cisco Blogs
S
Secure Thoughts
Y
Y Combinator Blog
D
Docker
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
N
News and Events Feed by Topic
aimingoo的专栏
aimingoo的专栏
I
InfoQ
P
Palo Alto Networks Blog
F
Full Disclosure
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Heimdal Security Blog
G
Google Developers Blog
Webroot Blog
Webroot Blog
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
WordPress大学
WordPress大学
W
WeLiveSecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
Help Net Security
Help Net Security
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
The Last Watchdog
The Last Watchdog
TaoSecurity Blog
TaoSecurity Blog
博客园 - 三生石上(FineUI控件)
T
Threatpost
V
V2EX
AWS News Blog
AWS News Blog
O
OpenAI News
V
Visual Studio Blog

Moon's Blog

Java内存 | Moon's Blog Spring Data JPA | Moon's Blog Spring Boot的Java Config | Moon's Blog Java知识点 | Moon's Blog Spring Boot知识点 | Moon's Blog Java异常机制 | Moon's Blog Java的类加载器 | Moon's Blog JVM类加载机制 | Moon's Blog 单体架构与微服务架构 | Moon's Blog 424.替换后的最长重复字符 | Moon's Blog 888.公平的糖果棒交换 | Moon's Blog 839.相似字符串组 | Moon's Blog 778.水位上升的泳池中游泳 | Moon's Blog 1631.最小体力消耗路径 | Moon's Blog 724.寻找数组的中心索引 | Moon's Blog 使用Jackson库实现Java多态解析 | Moon's Blog Spring Boot+InfluxDB实现日志管理 | Moon's Blog Java多态+工厂模式实现服务调用 | Moon's Blog 为Spring Boot项目生成OpenAPI3.0文档 | Moon's Blog
使用Jackson库实现日期序列化 | Moon's Blog
Moon Lou · 2020-09-19 · via Moon's Blog

Jackson是一个Java的用来处理JSON格式数据的类库,性能非常好。在项目开发中,使用Jackson库的@JsonSerialize注解实现日期的自定义格式的序列化,这里做一个记录。

一、需求背景

对于一个Model层对象的Date字段,在序列化为JSON数据时,可以使用Jakson库的@JsonFormat注解来定义格式,比如:

@Data
public class TestClass {
@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
private Date testTime;
}

在项目的实际开发过程中,这个日期可能不是Date,而是其他格式,比如Instant,此时再使用@JsonFormat注解就会报错。此时可以通过继承StdSerializer抽象类@JsonSerialize注解来实现自定义格式的序列化。

二、源代码

1. Instant序列化类InstantSerializer.java

public class InstantSerializer extends StdSerializer<Instant> {

private static DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

public InstantSerializer() {
this(null);
}

public InstantSerializer(Class<Instant> t) {
super(t);
}

@Override
public void serialize(Instant instant, JsonGenerator jsonGenerator, SerializerProvider provider)
throws IOException {
if (instant == null) {
return;
}
String jsonValue = format.format(instant.atZone(ZoneId.of("Asia/Shanghai")));
jsonGenerator.writeString(jsonValue);
}
}

InstantSerializer继承了StdSerializer抽象类,将Instant序列化为”yyyy-MM-dd HH:mm:ss”的日期格式。

2. String序列化类UTCDaySerializer.java

public class UTCDaySerializer extends StdSerializer<String> {

public UTCDaySerializer() {
this(null);
}

public UTCDaySerializer(Class<String> t) {
super(t);
}

@Override
public void serialize(String timeString, JsonGenerator jsonGenerator, SerializerProvider provider)
throws IOException {
if (timeString == null) {
return;
}
int index = timeString.indexOf("T");
String jsonValue = timeString.substring(0, index);
jsonGenerator.writeString(jsonValue);
}
}

UTCDaySerializer也继承了StdSerializer抽象类,将UTC格式的String日期,即”yyyy-MM-dd’T’HH:mm:ss’Z’”截取前半段,即”yyyy-MM-dd”的日期格式。

3. LogStatusDTO.java

@Data
@Builder
public class LogStatusDTO {
@JsonSerialize(using = InstantSerializer.class)
private Instant timestamp;

private String content;
}

使用@JsonSerialize注解和InstantSerializer来序列化Instant类型的timestamp字段。

4. ServiceLogNumPerDayDTO.java

@Data
@Measurement(name = "log")
public class ServiceLogNumPerDayDTO {
@Column(name = "time")
@JsonSerialize(using = UTCDaySerializer.class)
String date;

@Column(name = "count_content")
Integer count;
}

使用@JsonSerialize注解和UTCDaySerializer来序列化String类型的date字段。

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Moon's Blog