Spring rest接口中的LocalDateTime日期类型转时间戳
作者:QiHY 时间:2022-11-20 14:00:00
本文介绍spring-rest接口中的LocalDateTime日期类型转时间戳的方法。
具体的代码参照
示例项目 https://github.com/qihaiyan/springcamp/tree/master/spring-localdatetime-epoch
一、概述
java程序中一般将日期类型定义为LocalDateTime,数据库中保存的时间是0时区的时间(UTC时间)。对于接口来说,为了支持全球化多时区,接口中的日期类型通常会返回UTC时间戳,简称Epoch,数据类型为long,前端程序会根据本地时区,将时间戳转换为日期格式的字符串,如YYYY-mm-dd HH:mm:ss。
如果在每个时间型字段在接口返回时都进行转换处理,会比较繁琐。应该在一个统一的地方处理这种转换,业务逻辑处理过程中不感知这种转换。
二、通过Jackson2ObjectMapperBuilderCustomizer进行全局类型转换
spring提供了Jackson2ObjectMapperBuilderCustomizer
可以用于自定义json与对象之间相互转换的处理。
通过自定义Jackson2ObjectMapperBuilderCustomizer
,我们可以在json与对象的相互转换转换阶段完成LocalDateTime和Epoch之间的转换,包括接口的入参和出参。
@Configuration
public class LocalDateTimeToEpochSerdeConfig {
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> builder.serializerByType(LocalDateTime.class, new LocalDateTimeToEpochSerializer())
.deserializerByType(LocalDateTime.class, new LocalDateTimeFromEpochDeserializer());
}
/**
* 序列化
*/
public static class LocalDateTimeToEpochSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers)
throws IOException {
if (value != null) {
long timestamp = value.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
gen.writeNumber(timestamp);
}
}
}
/**
* 反序列化
*/
public static class LocalDateTimeFromEpochDeserializer extends JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
NumberDeserializers.LongDeserializer longDeserializer = new NumberDeserializers.LongDeserializer(Long.TYPE, 0L);
Long epoch = longDeserializer.deserialize(p, ctxt);
return LocalDateTime.ofInstant(Instant.ofEpochSecond(epoch), ZoneId.systemDefault());
}
}
}
以上代码中分别包含了json的序列化和反序列化操作,在序列化操作中,把LocalDateTime转换为Epoch。
/**
* 序列化
*/
public static class LocalDateTimeToEpochSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers)
throws IOException {
if (value != null) {
long timestamp = value.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
gen.writeNumber(timestamp);
}
}
}
在反序列化操作中,把Epoch转换为LocalDateTime。
/**
* 反序列化
*/
public static class LocalDateTimeFromEpochDeserializer extends JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
NumberDeserializers.LongDeserializer longDeserializer = new NumberDeserializers.LongDeserializer(Long.TYPE, 0L);
Long epoch = longDeserializer.deserialize(p, ctxt);
return LocalDateTime.ofInstant(Instant.ofEpochSecond(epoch), ZoneId.systemDefault());
}
}
通过以上配置,我们可以在实体类中使用LocalDateTime类型。客户端请求接口时,对于返回结果,自动转换为Epoch数据,对于请求参数,自动从Epoch转换为LocalDateTime。
来源:https://blog.csdn.net/haiyan_qi/article/details/123958390
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入探讨Linux静态库与动态库的详解(一看就懂)
Android开发中给EditText控件添加TextWatcher监听实现对输入字数的限制(推荐)
Spring boot动态修改日志级别的方法
![](https://img.aspxhome.com/file/2023/3/79853_0s.png)
springboot集成mybatisplus的方法
![](https://img.aspxhome.com/file/2023/9/64479_0s.png)
Java 延迟队列的常用的实现方式
![](https://img.aspxhome.com/file/2023/1/118901_0s.png)
Java遗传算法之冲出迷宫
使用java采集京东商城行政区划数据示例
Session过期后自动跳转到登录页面的实例代码
mybatis Interceptor对UpdateTime自动处理的实现方法
mybatis 实体类字段大小写问题 字段获取不到值的解决
![](https://img.aspxhome.com/file/2023/9/101989_0s.png)
SpringBoot2.x 集成 Thymeleaf的详细教程
![](https://img.aspxhome.com/file/2023/1/114041_0s.png)
Java中Collection、List、Set、Map之间的关系总结
如何解决Spring in action @valid验证不生效的问题
Android监听键盘状态获取键盘高度的实现方法
java中如何执行xshell命令
OpenGL绘制三次Bezier曲线
![](https://img.aspxhome.com/file/2023/3/112283_0s.jpg)
android动态设置app当前运行语言的方法
![](https://img.aspxhome.com/file/2023/1/100611_0s.jpg)
Android开发登陆案例
![](https://img.aspxhome.com/file/2023/3/137743_0s.png)
Android 开发中layout下的子文件夹
![](https://img.aspxhome.com/file/2023/7/128957_0s.png)
Android RecyclerView实现滑动删除
![](https://img.aspxhome.com/file/2023/5/139335_0s.jpg)