SpringBoot中Jackson日期格式化技巧分享
Jackson 日期格式化技巧
使用 Spring Boot 時(shí),需要使用 Jackson 處理一些 Java Time API 類型的 JSON 序列化問(wèn)題,在處理一些類的字段時(shí),可以通過(guò)直接在屬性上加注解的方式來(lái)指定其格式化樣式。但是,昨天同事遇到一個(gè)格式化 Map 數(shù)據(jù)的問(wèn)題,這樣就不能通過(guò)加注解來(lái)解決格式化樣式的問(wèn)題了。
在網(wǎng)上各種搜索,各種嘗試后,終于解決了這個(gè)問(wèn)題,記錄一下,以備不時(shí)之需。
閑言少敘,直接上代碼:
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 對(duì)象,配置日期格式化
*
* @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ù)問(wèn)題
不知道通過(guò)這種方式指定日期格式化樣式后,在處理一些打格式化樣式注解的字段時(shí),會(huì)有什么樣的表現(xiàn)?有機(jī)會(huì)測(cè)試一下。
補(bǔ)充: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類型會(huì)被格式化成配置中的格式。
方式二:配置類中配置創(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot將多個(gè)文件夾進(jìn)行壓縮的兩種方法(瀏覽器下載和另存為)
Spring Boot項(xiàng)目通常不會(huì)自動(dòng)對(duì)文件夾進(jìn)行壓縮,不過(guò),在打包應(yīng)用時(shí),如果你使用了Maven或Gradle這樣的構(gòu)建工具,并且配置了相應(yīng)的插件,可以在打成jar或war包的時(shí)候?qū)⒁蕾嚨膸?kù)文件合并并壓縮,本文介紹了SpringBoot將多個(gè)文件夾進(jìn)行壓縮的兩種方法2024-07-07
Spring中11個(gè)最常用的擴(kuò)展點(diǎn)總結(jié),你知道幾個(gè)
我們知道IOC(控制反轉(zhuǎn))和AOP(面向切面編程)是spring的基石,除此之外spring的擴(kuò)展能力非常強(qiáng),下面這篇文章主要給大家介紹了關(guān)于Spring中11個(gè)最常用的擴(kuò)展點(diǎn)的相關(guān)資料,需要的朋友可以參考下2022-12-12
Maven中plugins與pluginManagement的區(qū)別說(shuō)明
這篇文章主要介紹了Maven中plugins與pluginManagement的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
啟用Spring事務(wù)管理@EnableTransactionManagement示例解析
這篇文章主要為大家介紹了啟用Spring事務(wù)管理@EnableTransactionManagement示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
SpringCloud中的Ribbon負(fù)載均衡詳細(xì)解讀
這篇文章主要介紹了SpringCloud中的Ribbon負(fù)載均衡詳細(xì)解讀,當(dāng)系統(tǒng)面臨大量的用戶訪問(wèn),負(fù)載過(guò)高的時(shí)候,通常會(huì)增加服務(wù)器數(shù)量來(lái)進(jìn)行橫向擴(kuò)展(集群),多個(gè)服務(wù)器的負(fù)載需要均衡,以免出現(xiàn)服務(wù)器負(fù)載不均衡,部分服務(wù)器負(fù)載較大,部分服務(wù)器負(fù)載較小的情況,需要的朋友可以參考下2023-11-11
IDEA之web項(xiàng)目導(dǎo)入jar包方式
這篇文章主要介紹了IDEA之web項(xiàng)目導(dǎo)入jar包方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
SpringBoot快速構(gòu)建應(yīng)用程序方法介紹
這篇文章主要介紹了SpringBoot快速構(gòu)建應(yīng)用程序方法介紹,涉及SpringBoot默認(rèn)的錯(cuò)誤頁(yè)面,嵌入式Web容器層面的約定和定制等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-11-11
java IO流 之 輸入流 InputString()的使用
這篇文章主要介紹了java IO流 之 輸入流 InputString()的使用,以及讀取數(shù)據(jù)的三種方式詳解,非常不錯(cuò),需要的朋友可以參考下2016-12-12

