欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

具體的代碼參照

示例項(xiàng)目 https://github.com/qihaiyan/springcamp/tree/master/spring-localdatetime-epoch

一、概述

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

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

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

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

通過(guò)自定義Jackson2ObjectMapperBuilderCustomizer,我們可以在json與對(duì)象的相互轉(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)換為L(zhǎng)ocalDateTime。

    /**
     * 反序列化
     */
    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());
        }
    }

通過(guò)以上配置,我們可以在實(shí)體類中使用LocalDateTime類型??蛻舳苏?qǐng)求接口時(shí),對(duì)于返回結(jié)果,自動(dòng)轉(zhuǎn)換為Epoch數(shù)據(jù),對(duì)于請(qǐng)求參數(shù),自動(dòng)從Epoch轉(zhuǎn)換為L(zhǎng)ocalDateTime。

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

相關(guān)文章

最新評(píng)論