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ù)庫存儲(chǔ)時(shí)間類型的數(shù)據(jù)使用了datetime類型,那么后端查出來的數(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;
如果以上方法還是不能解決問題,可嘗試使用:
@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)文章
在IntelliJ IDEA 搭建springmvc項(xiàng)目配置debug的教程詳解
這篇文章主要介紹了在IntelliJ IDEA 搭建springmvc項(xiàng)目配置debug的教程詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Spring Security 自定義短信登錄認(rèn)證的實(shí)現(xiàn)
這篇文章主要介紹了Spring Security 自定義短信登錄認(rèn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Java底層基于鏈表實(shí)現(xiàn)集合和映射--集合Set操作詳解
這篇文章主要介紹了Java底層基于鏈表實(shí)現(xiàn)集合和映射集合Set操作,結(jié)合實(shí)例形式詳細(xì)分析了Java使用鏈表實(shí)現(xiàn)集合和映射相關(guān)原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-03-03
SpringBoot關(guān)于自定義注解實(shí)現(xiàn)接口冪等性方式
這篇文章主要介紹了SpringBoot關(guān)于自定義注解實(shí)現(xiàn)接口冪等性方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
淺談springioc實(shí)例化bean的三個(gè)方法
下面小編就為大家?guī)硪黄獪\談springioc實(shí)例化bean的三個(gè)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就想給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09

