当前位置:网站首页>Using jsonserialize to realize data type conversion gracefully
Using jsonserialize to realize data type conversion gracefully
2022-04-23 03:20:00 【Small planes love traveling】
One 、
Requirements introduction : I hope that through annotation , Batch time format In the expected format .
JsonSerialize brief introduction :com.fasterxml.jackson.databind.annotation Serialization annotation provided by package
What is serialization :
Encode an object into a byte stream (I/O); The opposite operation is called deserialization .
The purpose of serialization is to facilitate the transfer of data and storage on disk ( Put one Java Objects are written to the hard disk or transferred to other computers on the network , At this time, we need to convert the object into a byte stream for network transmission . For this kind of general operation , Serialization appears to unify these formats )
Two 、 actual combat
1、 First, customize a class inheritance JsonSerializer To rewrite serialize Method , It implements the specific data type conversion logic
@Slf4j
public class DataFormatSerializeer extends JsonSerializer<String> {
@Override
public void serialize(String dateStr, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider) throws IOException {
if (StringUtils.isNotBlank(dateStr)) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date date = null;
try {
date = dateFormat.parse(dateStr);
Timestamp time = new Timestamp(date.getTime());
jsonGenerator.writeString(DateUtils.convertSqlTimeStampToUtc(time, "UTC-4",
DateUtils.ISO8601_DATE_TIME_FORMAT));
} catch (ParseException e) {
log.error("date convert error");
}
} else {
jsonGenerator.writeString("");
}
}
}
2. application
@JsonSerialize(using = DataFormatSerializeer.class)// Here we refer to our custom class
private String date;
版权声明
本文为[Small planes love traveling]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220623382268.html
边栏推荐
- 二进制文件版本控制工具选择难?看完这篇你会找到答案
- Using swagger in. Net5
- OLED multi-level menu record
- Mysql database
- A comprehensive understanding of static code analysis
- JSON related
- Charles uses three ways to modify requests and responses
- Mysql database design specification
- MySQL query specifies that a row is sorted to the first row
- 2022A特种设备相关管理(电梯)上岗证题库及模拟考试
猜你喜欢

MySQL query specifies that a row is sorted to the first row

Experiment 5 components and event handling

Xamarin effect Chapter 22 recording effect

. net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure

Explanation keyword of MySQL

C language to achieve address book - (static version)

Fight leetcode again (290. Word law)

Configure automatic implementation of curd projects

It can receive multiple data type parameters - variable parameters

一套组合拳,打造一款 IDEA 护眼方案
随机推荐
Swap the left and right of each node in a binary tree
be based on. NETCORE development blog project starblog - (2) environment preparation and creation project
Top ten project management software similar to JIRA
关于idea调试模式下启动特别慢的优化
There is no index in the database table. When inserting data, SQL statements are used to prevent repeated addition (Reprint)
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
全新的ORM框架——BeetlSQL介绍
[Mysql] LEFT函数 | RIGHT函数
Node configuration environment CMD does not take effect
Supersocket is Use in net5 - concept
New ORM framework -- Introduction to beetlsql
Advanced sorting - fast sorting
[untitled]
IDEA查看历史记录【文件历史和项目历史】
Idempotency practice operation, explaining idempotency based on business
Using swagger in. Net5
Peut recevoir plusieurs paramètres de type de données - paramètres variables
General test technology [II] test method
2022 P cylinder filling training test questions and simulation test
MySQL keyword group_ Concat, combined connection query