亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring rest接口中的LocalDateTime日期類型轉(zhuǎn)時間戳

 更新時間:2023年03月28日 10:20:59   作者:QiHY  
這篇文章主要介紹了Spring rest接口中的LocalDateTime日期類型轉(zhuǎn)時間戳的方法,Java程序中一般將日期類型定義為LocalDateTime,數(shù)據(jù)庫中保存的時間是0時區(qū)的時間

本文介紹spring-rest接口中的LocalDateTime日期類型轉(zhuǎn)時間戳的方法。

具體的代碼參照

示例項目 https://github.com/qihaiyan/springcamp/tree/master/spring-localdatetime-epoch

一、概述

java程序中一般將日期類型定義為LocalDateTime,數(shù)據(jù)庫中保存的時間是0時區(qū)的時間(UTC時間)。對于接口來說,為了支持全球化多時區(qū),接口中的日期類型通常會返回UTC時間戳,簡稱Epoch,數(shù)據(jù)類型為long,前端程序會根據(jù)本地時區(qū),將時間戳轉(zhuǎn)換為日期格式的字符串,如YYYY-mm-dd HH:mm:ss。

如果在每個時間型字段在接口返回時都進行轉(zhuǎn)換處理,會比較繁瑣。應(yīng)該在一個統(tǒng)一的地方處理這種轉(zhuǎn)換,業(yè)務(wù)邏輯處理過程中不感知這種轉(zhuǎn)換。

二、通過Jackson2ObjectMapperBuilderCustomizer進行全局類型轉(zhuǎn)換

spring提供了Jackson2ObjectMapperBuilderCustomizer可以用于自定義json與對象之間相互轉(zhuǎn)換的處理。

通過自定義Jackson2ObjectMapperBuilderCustomizer,我們可以在json與對象的相互轉(zhuǎn)換轉(zhuǎn)換階段完成LocalDateTime和Epoch之間的轉(zhuǎn)換,包括接口的入?yún)⒑统鰠ⅰ?/p>

@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轉(zhuǎn)換為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轉(zhuǎn)換為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類型。客戶端請求接口時,對于返回結(jié)果,自動轉(zhuǎn)換為Epoch數(shù)據(jù),對于請求參數(shù),自動從Epoch轉(zhuǎn)換為LocalDateTime。

到此這篇關(guān)于Spring rest接口中的LocalDateTime日期類型轉(zhuǎn)時間戳的文章就介紹到這了,更多相關(guān)Spring LocalDateTime內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論