您好,登錄后才能下訂單哦!
這篇“Spring rest接口中的LocalDateTime日期類型轉(zhuǎn)時(shí)間戳怎么實(shí)現(xiàn)”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“Spring rest接口中的LocalDateTime日期類型轉(zhuǎn)時(shí)間戳怎么實(shí)現(xiàn)”文章吧。
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)換。
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í)間戳怎么實(shí)現(xiàn)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。