springboot中如何配置LocalDateTime JSON返回時(shí)間戳
springboot配置LocalDateTime JSON返回時(shí)間戳
方案一
創(chuàng)建配置類
注意:使用這種方式,所有Controller的JSON數(shù)據(jù)返回,只要是LocalDateTime類型都會(huì)被轉(zhuǎn)成時(shí)間戳
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import java.io.IOException; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; /** * @author jamesluozhiwei * @date 2019/12/18 */ @Configuration public class LocalDateTimeSerializerConfig { /** * 序列化LocalDateTime * @return */ @Bean @Primary public ObjectMapper serializingObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()); objectMapper.registerModule(javaTimeModule); return objectMapper; } /** * 序列化實(shí)現(xiàn) */ public static class LocalDateTimeSerializer 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().toEpochMilli(); gen.writeNumber(timestamp); } } } /** * 反序列化實(shí)現(xiàn) */ public static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { @Override public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException { long timestamp = p.getValueAsLong(); if (timestamp > 0){ return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp),ZoneId.systemDefault()); }else{ return null; } } } }
方案二
和方案一 一樣都是實(shí)現(xiàn)序列化和反序列化接口
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import java.io.IOException; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; /** * @author jamesluozhiwei * @date 2019/12/18 */ @Configuration public class LocalDateTimeSerializerConfig { @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> { builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer()); builder.deserializerByType(LocalDateTime.class,new LocalDateTimeDeserializer()); }; } /** * 序列化 */ public static class LocalDateTimeSerializer 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().toEpochMilli(); gen.writeNumber(timestamp); } } } /** * 反序列化 */ public static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { @Override public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException { long timestamp = p.getValueAsLong(); if (timestamp > 0){ return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp),ZoneId.systemDefault()); }else{ return null; } } } }
springboot中的時(shí)間以Json返回格式不正確的解決
如果后端數(shù)據(jù)庫(kù)存儲(chǔ)時(shí)間類型的數(shù)據(jù)使用了datetime類型,那么后端查出來(lái)的數(shù)據(jù)就是LocalDateTime類型,此時(shí)若以Json傳給前端,那么前端接收到的時(shí)間里就會(huì)有“T”,此時(shí)只要在傳參上加上標(biāo)簽即可:
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createTime;
而如果傳給前端的是LocalDate的時(shí)間類型,前端接收到的時(shí)間也是不正常的,此時(shí)需要加上標(biāo)簽處理:
@JsonFormat(pattern = "yyyy-MM-dd") private LocalDate bizTime;
如果以上方法還是不能解決問(wèn)題,可嘗試使用:
@JSONField(format="yyyy-MM-dd HH:mm:ss")
總結(jié)
如果你有更好的方法,可以相互討論學(xué)習(xí)一下~
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換
- Java?LocalDateTime獲取時(shí)間信息、格式化、轉(zhuǎn)換為數(shù)字時(shí)間戳代碼示例
- JAVA中時(shí)間戳與LocalDateTime互相轉(zhuǎn)換代碼例子
- Spring rest接口中的LocalDateTime日期類型轉(zhuǎn)時(shí)間戳
- java傳入時(shí)間戳返回LocalDateTime的實(shí)現(xiàn)方法
- Java中Date、LocalDate、LocalDateTime、LocalTime、時(shí)間戳之間的相互轉(zhuǎn)換代碼
相關(guān)文章
Spring?Boot集成etcd的詳細(xì)過(guò)程
etcd是一個(gè)分布式鍵值存儲(chǔ)數(shù)據(jù)庫(kù),用于共享配置和服務(wù)發(fā)現(xiàn),etcd采用Go語(yǔ)言編寫,具有出色的跨平臺(tái)支持,很小的二進(jìn)制文件和強(qiáng)大的社區(qū),這篇文章主要介紹了SpringBoot集成etcd,需要的朋友可以參考下2023-08-08springboot使用定時(shí)器@Scheduled不管用的解決
這篇文章主要介紹了springboot使用定時(shí)器@Scheduled不管用的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12SpringMVC結(jié)合模板模式實(shí)現(xiàn)MyBatisPlus傳遞嵌套JSON數(shù)據(jù)
我們經(jīng)常會(huì)遇到需要傳遞對(duì)象的場(chǎng)景,有時(shí)候,我們需要將一個(gè)對(duì)象的數(shù)據(jù)傳遞給另一個(gè)對(duì)象進(jìn)行處理,但是又不希望直接暴露對(duì)象的內(nèi)部結(jié)構(gòu)和實(shí)現(xiàn)細(xì)節(jié),所以本文給大家介紹了SpringMVC結(jié)合模板模式實(shí)現(xiàn)MyBatisPlus傳遞嵌套JSON數(shù)據(jù),需要的朋友可以參考下2024-03-03javaweb圖書商城設(shè)計(jì)之分類模塊(2)
這篇文章主要為大家詳細(xì)介紹了javaweb圖書商城設(shè)計(jì)之分類模塊的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11解決mapper接口無(wú)法映射mapper.xml的問(wèn)題
這篇文章主要介紹了解決mapper接口無(wú)法映射mapper.xml的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06java 二進(jìn)制數(shù)據(jù)與16進(jìn)制字符串相互轉(zhuǎn)化方法
今天小編就為大家分享一篇java 二進(jìn)制數(shù)據(jù)與16進(jìn)制字符串相互轉(zhuǎn)化方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07探索Java中的equals()和hashCode()方法_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了探索Java中的equals()和hashCode()方法的相關(guān)資料,需要的朋友可以參考下2017-05-05