springboot中如何配置LocalDateTime JSON返回時間戳
springboot配置LocalDateTime JSON返回時間戳
方案一
創(chuàng)建配置類
注意:使用這種方式,所有Controller的JSON數(shù)據(jù)返回,只要是LocalDateTime類型都會被轉(zhuǎ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.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中的時間以Json返回格式不正確的解決
如果后端數(shù)據(jù)庫存儲時間類型的數(shù)據(jù)使用了datetime類型,那么后端查出來的數(shù)據(jù)就是LocalDateTime類型,此時若以Json傳給前端,那么前端接收到的時間里就會有“T”,此時只要在傳參上加上標(biāo)簽即可:
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createTime;
而如果傳給前端的是LocalDate的時間類型,前端接收到的時間也是不正常的,此時需要加上標(biāo)簽處理:
@JsonFormat(pattern = "yyyy-MM-dd") private LocalDate bizTime;
如果以上方法還是不能解決問題,可嘗試使用:
@JSONField(format="yyyy-MM-dd HH:mm:ss")
總結(jié)
如果你有更好的方法,可以相互討論學(xué)習(xí)一下~
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot使用定時器@Scheduled不管用的解決
這篇文章主要介紹了springboot使用定時器@Scheduled不管用的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12SpringMVC結(jié)合模板模式實(shí)現(xiàn)MyBatisPlus傳遞嵌套JSON數(shù)據(jù)
我們經(jīng)常會遇到需要傳遞對象的場景,有時候,我們需要將一個對象的數(shù)據(jù)傳遞給另一個對象進(jìn)行處理,但是又不希望直接暴露對象的內(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-11java 二進(jìn)制數(shù)據(jù)與16進(jìn)制字符串相互轉(zhuǎn)化方法
今天小編就為大家分享一篇java 二進(jìn)制數(shù)據(jù)與16進(jìn)制字符串相互轉(zhuǎn)化方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07探索Java中的equals()和hashCode()方法_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了探索Java中的equals()和hashCode()方法的相關(guān)資料,需要的朋友可以參考下2017-05-05