SpringBoot中Jackson日期格式化技巧分享
Jackson 日期格式化技巧
使用 Spring Boot 時,需要使用 Jackson 處理一些 Java Time API 類型的 JSON 序列化問題,在處理一些類的字段時,可以通過直接在屬性上加注解的方式來指定其格式化樣式。但是,昨天同事遇到一個格式化 Map 數(shù)據(jù)的問題,這樣就不能通過加注解來解決格式化樣式的問題了。
在網(wǎng)上各種搜索,各種嘗試后,終于解決了這個問題,記錄一下,以備不時之需。
閑言少敘,直接上代碼:
package com.diguage.demo.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import static com.fasterxml.jackson.databind.SerializationFeature.*;
import static java.time.format.DateTimeFormatter.ofPattern;
/<strong>
* 配置類
*
* @author D瓜哥 · <a rel="external nofollow" rel="external nofollow" target="_blank" >https://www.diguage.com</a>
*/
@Configuration
public class Config {
/</strong>
* 創(chuàng)建 ObjectMapper 對象,配置日期格式化
*
* @author D瓜哥 · <a rel="external nofollow" rel="external nofollow" target="_blank" >https://www.diguage.com</a>
*/
@Bean
@Primary
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
String dateTimepattern = "yyyy-MM-dd HH:mm:ss";
String datePattern = "yyyy-MM-dd";
DateFormat dateFormat = new SimpleDateFormat(dateTimepattern);
mapper.setDateFormat(dateFormat);
mapper.configure(WRITE_DATES_AS_TIMESTAMPS, false);
mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true));
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addDeserializer(LocalDate.class,
new LocalDateDeserializer(ofPattern(datePattern)));
javaTimeModule.addSerializer(LocalDate.class,
new LocalDateSerializer(ofPattern(datePattern)));
javaTimeModule.addDeserializer(LocalDateTime.class,
new LocalDateTimeDeserializer(ofPattern(dateTimepattern)));
javaTimeModule.addSerializer(LocalDateTime.class,
new LocalDateTimeSerializer(ofPattern(dateTimepattern)));
mapper.registerModule(javaTimeModule);
return mapper;
}
}后續(xù)問題
不知道通過這種方式指定日期格式化樣式后,在處理一些打格式化樣式注解的字段時,會有什么樣的表現(xiàn)?有機會測試一下。
補充:Jackson 統(tǒng)一配置 日期轉(zhuǎn)換格式
方式一:配置文件yml中配置
spring:
jackson:
default-property-inclusion: ALWAYS
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
這樣序列化后,Date類型會被格式化成配置中的格式。
方式二:配置類中配置創(chuàng)建JacksonConfig.java
@Configuration
public class JacksonConfig {
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public Jackson2ObjectMapperBuilderCustomizer customJackson() {
return new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(Jackson2ObjectMapperBuilder builder) {
builder.serializerByType(LocalDateTime.class,
new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
builder.serializerByType(LocalDate.class,
new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
builder.serializerByType(LocalTime.class,
new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
builder.deserializerByType(LocalDateTime.class,
new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
builder.deserializerByType(LocalDate.class,
new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
builder.deserializerByType(LocalTime.class,
new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
builder.failOnUnknownProperties(false);
builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
};
}
}參考資料
到此這篇關(guān)于SpringBoot中Jackson日期格式化技巧的文章就介紹到這了,更多相關(guān)SpringBoot Jackson日期格式化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot將多個文件夾進行壓縮的兩種方法(瀏覽器下載和另存為)
Spring Boot項目通常不會自動對文件夾進行壓縮,不過,在打包應(yīng)用時,如果你使用了Maven或Gradle這樣的構(gòu)建工具,并且配置了相應(yīng)的插件,可以在打成jar或war包的時候?qū)⒁蕾嚨膸煳募喜⒉嚎s,本文介紹了SpringBoot將多個文件夾進行壓縮的兩種方法2024-07-07
Spring中11個最常用的擴展點總結(jié),你知道幾個
我們知道IOC(控制反轉(zhuǎn))和AOP(面向切面編程)是spring的基石,除此之外spring的擴展能力非常強,下面這篇文章主要給大家介紹了關(guān)于Spring中11個最常用的擴展點的相關(guān)資料,需要的朋友可以參考下2022-12-12
Maven中plugins與pluginManagement的區(qū)別說明
這篇文章主要介紹了Maven中plugins與pluginManagement的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
啟用Spring事務(wù)管理@EnableTransactionManagement示例解析
這篇文章主要為大家介紹了啟用Spring事務(wù)管理@EnableTransactionManagement示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
SpringBoot快速構(gòu)建應(yīng)用程序方法介紹
這篇文章主要介紹了SpringBoot快速構(gòu)建應(yīng)用程序方法介紹,涉及SpringBoot默認的錯誤頁面,嵌入式Web容器層面的約定和定制等相關(guān)內(nèi)容,具有一定借鑒價值,需要的朋友可以參考下。2017-11-11
java IO流 之 輸入流 InputString()的使用
這篇文章主要介紹了java IO流 之 輸入流 InputString()的使用,以及讀取數(shù)據(jù)的三種方式詳解,非常不錯,需要的朋友可以參考下2016-12-12

