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

标签:Spring,LocalDateTime,rest,时间戳
0
投稿

猜你喜欢

  • 深入探讨Linux静态库与动态库的详解(一看就懂)

    2023-07-04 01:02:28
  • Android开发中给EditText控件添加TextWatcher监听实现对输入字数的限制(推荐)

    2023-01-19 23:52:35
  • Spring boot动态修改日志级别的方法

    2023-04-04 09:36:00
  • springboot集成mybatisplus的方法

    2022-08-02 16:35:12
  • Java 延迟队列的常用的实现方式

    2022-06-30 13:57:00
  • Java遗传算法之冲出迷宫

    2022-01-12 21:34:58
  • 使用java采集京东商城行政区划数据示例

    2023-04-17 06:31:52
  • Session过期后自动跳转到登录页面的实例代码

    2022-01-30 13:48:56
  • mybatis Interceptor对UpdateTime自动处理的实现方法

    2023-10-13 16:02:20
  • mybatis 实体类字段大小写问题 字段获取不到值的解决

    2021-06-29 07:44:58
  • SpringBoot2.x 集成 Thymeleaf的详细教程

    2021-07-31 18:18:23
  • Java中Collection、List、Set、Map之间的关系总结

    2022-11-18 21:41:42
  • 如何解决Spring in action @valid验证不生效的问题

    2023-08-29 07:59:56
  • Android监听键盘状态获取键盘高度的实现方法

    2023-12-02 16:44:17
  • java中如何执行xshell命令

    2021-10-06 16:22:18
  • OpenGL绘制三次Bezier曲线

    2022-04-23 18:18:14
  • android动态设置app当前运行语言的方法

    2022-02-21 01:33:37
  • Android开发登陆案例

    2022-03-19 10:13:01
  • Android 开发中layout下的子文件夹

    2021-10-03 16:10:44
  • Android RecyclerView实现滑动删除

    2022-11-20 19:50:42
  • asp之家 软件编程 m.aspxhome.com