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)?有機(jī)會測試一下。
補(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類型會被格式化成配置中的格式。
方式二:配置類中配置創(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)文章
淺談springMVC接收前端json數(shù)據(jù)的總結(jié)
下面小編就為大家分享一篇淺談springMVC接收前端json數(shù)據(jù)的總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03java代碼抓取網(wǎng)頁郵箱的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava代碼抓取網(wǎng)頁郵箱的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06Springmvc獲取前臺請求數(shù)據(jù)過程解析
這篇文章主要介紹了Springmvc獲取前臺請求數(shù)據(jù)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07利用Java簡單實(shí)現(xiàn)一個代碼行數(shù)統(tǒng)計器方法實(shí)例
這篇文章主要給大家介紹了關(guān)于如何利用Java簡單實(shí)現(xiàn)一個代碼行數(shù)統(tǒng)計器的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Java Elastic Job動態(tài)添加任務(wù)實(shí)現(xiàn)過程解析
這篇文章主要介紹了Java Elastic Job動態(tài)添加任務(wù)實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08Java數(shù)據(jù)庫連接PreparedStatement的使用詳解
這篇文章主要介紹了Java數(shù)據(jù)庫連接PreparedStatement的使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08Java程序員的10道常見的XML面試問答題(XML術(shù)語詳解)
包括web開發(fā)人員的Java面試在內(nèi)的各種面試中,XML面試題在各種編程工作的面試中很常見。XML是一種成熟的技術(shù),經(jīng)常作為從一個平臺到其他平臺傳輸數(shù)據(jù)的標(biāo)準(zhǔn)2014-04-04Java 實(shí)戰(zhàn)項(xiàng)目錘煉之醫(yī)院門診收費(fèi)管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+html+jdbc+mysql實(shí)現(xiàn)一個醫(yī)院門診收費(fèi)管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11