Spring rest接口中的LocalDateTime日期類型轉(zhuǎn)時(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ù)庫中保存的時(shí)間是0時(shí)區(qū)的時(shí)間(UTC時(shí)間)。對(duì)于接口來說,為了支持全球化多時(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ù)邏輯處理過程中不感知這種轉(zhuǎn)換。
二、通過Jackson2ObjectMapperBuilderCustomizer進(jìn)行全局類型轉(zhuǎn)換
spring提供了Jackson2ObjectMapperBuilderCustomizer可以用于自定義json與對(duì)象之間相互轉(zhuǎn)換的處理。
通過自定義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());
}
}
通過以上配置,我們可以在實(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)文章希望大家以后多多支持腳本之家!
- Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換
- Java?LocalDateTime獲取時(shí)間信息、格式化、轉(zhuǎn)換為數(shù)字時(shí)間戳代碼示例
- JAVA中時(shí)間戳與LocalDateTime互相轉(zhuǎn)換代碼例子
- springboot中如何配置LocalDateTime JSON返回時(shí)間戳
- java傳入時(shí)間戳返回LocalDateTime的實(shí)現(xiàn)方法
- Java中Date、LocalDate、LocalDateTime、LocalTime、時(shí)間戳之間的相互轉(zhuǎn)換代碼
相關(guān)文章
基于SpringBoot實(shí)現(xiàn)上傳2種方法工程代碼實(shí)例
這篇文章主要介紹了基于SpringBoot實(shí)現(xiàn)上傳工程代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
java之super關(guān)鍵字用法實(shí)例解析
這篇文章主要介紹了java之super關(guān)鍵字用法實(shí)例解析,較為詳細(xì)的分析了super關(guān)鍵字的用法及內(nèi)存分布,需要的朋友可以參考下2014-09-09
java接口用戶上下文的設(shè)計(jì)與實(shí)現(xiàn)
這篇文章主要為大家介紹了接口用戶上下文的設(shè)計(jì)與實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
IDEA使用SpringAssistant插件創(chuàng)建SpringCloud項(xiàng)目
IDEA 功能強(qiáng)大,可以用來高效的開發(fā)應(yīng)該程序。它還支持第三方插件、用戶可以根據(jù)需要添加自己喜歡的插件。下面介紹如何使用 IDEA 創(chuàng)建 Spring Cloud 項(xiàng)目2021-06-06
手把手教你使用IDEA創(chuàng)建多模塊(maven)項(xiàng)目
這篇文章主要給大家介紹了關(guān)于如何使用IDEA創(chuàng)建多模塊(maven)項(xiàng)目的相關(guān)資料,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
使用Spring的JAVA Mail支持簡(jiǎn)化郵件發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了使用Spring的JAVA Mail支持簡(jiǎn)化郵件發(fā)送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04

