springboot項(xiàng)目中統(tǒng)一時(shí)間格式處理方法
JacksonConfiguration:
主要用于配置 Jackson 的序列化和反序列化,影響 JSON 請(qǐng)求和響應(yīng)的內(nèi)容。適用于處理 JSON 數(shù)據(jù)的序列化和反序列化
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Configuration
public class JacksonConfiguration {
@Value("${spring.jackson.date-format}")
private String dateTimeFormat;
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> {
builder.deserializerByType(Long.TYPE, StringDeserializer.instance);
builder.serializerByType(Long.class, ToStringSerializer.instance);
builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
SimpleModule module = new SimpleModule();
module.addKeySerializer(LocalDateTime.class, new JsonSerializer<>() {
final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(dateTimeFormat);
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeFieldName(FORMATTER.format(value));
}
});
builder.modules(module);
};
}
}LocalDateTimeFormatter:
主要用于處理 Spring MVC 中的請(qǐng)求參數(shù)和響應(yīng)體中的 LocalDateTime。它直接影響 Controller 方法中參數(shù)的解析和返回值的格式化。
import org.springframework.context.annotation.Configuration;
import org.springframework.format.Formatter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
@SuppressWarnings("NullableProblems")
@Configuration
public class LocalDateTimeFormatter implements Formatter<LocalDateTime> {
public static final String DATA_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
public static final String DATA_TIME_FORMAT_T = "yyyy-MM-dd'T'HH:mm:ss.SSS";
@Override
public LocalDateTime parse(String text, Locale locale) {
return LocalDateTime.parse(text, DateTimeFormatter.ofPattern(DATA_TIME_FORMAT));
}
@Override
public String print(LocalDateTime object, Locale locale) {
return DateTimeFormatter.ofPattern(DATA_TIME_FORMAT).format(object);
}
}到此這篇關(guān)于springboot項(xiàng)目中統(tǒng)一時(shí)間格式處理的文章就介紹到這了,更多相關(guān)springboot統(tǒng)一時(shí)間格式處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nacos配置中心搭建及動(dòng)態(tài)刷新配置及踩坑記錄
這篇文章主要介紹了Nacos配置中心搭建及動(dòng)態(tài)刷新配置及踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Spring Aop之AspectJ注解配置實(shí)現(xiàn)日志管理的方法
下面小編就為大家分享一篇Spring Aop之AspectJ注解配置實(shí)現(xiàn)日志管理的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
java實(shí)體對(duì)象與Map之間的轉(zhuǎn)換工具類代碼實(shí)例
這篇文章主要介紹了java實(shí)體對(duì)象與Map之間的轉(zhuǎn)換工具類代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
SpringBoot定制JSON響應(yīng)數(shù)據(jù)的實(shí)現(xiàn)
本文主要介紹了SpringBoot定制JSON響應(yīng)數(shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02
Java基本數(shù)據(jù)類型之間的相互轉(zhuǎn)換詳解
這篇文章主要講解Java中基本數(shù)據(jù)類型的轉(zhuǎn)換,數(shù)據(jù)之間相互轉(zhuǎn)換是經(jīng)常會(huì)用到的基礎(chǔ)操作,文中講的很清晰,希望能給大家做一個(gè)參考。2022-05-05

