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ù)庫(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)文章希望大家以后多多支持腳本之家!
- 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)文章
Java Swing中的JButton、JComboBox、JList和JColorChooser組件使用案例
這篇文章主要介紹了Java Swing中的按鈕(JButton)、組合框(JComboBox)、下拉列表(JList)和顏色選擇器(JColorChooser)組件使用案例,需要的朋友可以參考下2014-10-10springboot全局字符編碼設(shè)置方式(解決亂碼問(wèn)題)
這篇文章主要介紹了springboot全局字符編碼設(shè)置方式(解決亂碼問(wèn)題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Java 3種方法實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換
這篇文章主要介紹了Java 3種方法實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換,幫助大家利用Java處理數(shù)據(jù),感興趣的朋友可以了解下2020-09-09如何從eureka獲取服務(wù)的ip和端口號(hào)進(jìn)行Http的調(diào)用
這篇文章主要介紹了如何從eureka獲取服務(wù)的ip和端口號(hào)進(jìn)行Http的調(diào)用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03SystemServer進(jìn)程啟動(dòng)過(guò)程解析
這篇文章主要為大家介紹了SystemServer進(jìn)程啟動(dòng)過(guò)程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07SpringBoot?中使用?Validation?校驗(yàn)參數(shù)的方法詳解
Validation?是用于檢查程序代碼中參數(shù)的有效性的框架,作為?Spring?框架中的一個(gè)參數(shù)校驗(yàn)工具,集成在?spring-context?包中,這篇文章主要介紹了SpringBoot?中使用?Validation?校驗(yàn)參數(shù),需要的朋友可以參考下2022-05-05