最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot中Jackson日期格式化技巧分享

 更新時間:2022年04月13日 15:28:01   作者:D瓜哥  
一般在SpringBoot項目中,spring默認使用jackson轉(zhuǎn)換日期,下面這篇文章主要給大家介紹了關(guān)于SpringBoot中Jackson日期格式化技巧的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下

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將多個文件夾進行壓縮的兩種方法(瀏覽器下載和另存為)

    SpringBoot將多個文件夾進行壓縮的兩種方法(瀏覽器下載和另存為)

    Spring Boot項目通常不會自動對文件夾進行壓縮,不過,在打包應(yīng)用時,如果你使用了Maven或Gradle這樣的構(gòu)建工具,并且配置了相應(yīng)的插件,可以在打成jar或war包的時候?qū)⒁蕾嚨膸煳募喜⒉嚎s,本文介紹了SpringBoot將多個文件夾進行壓縮的兩種方法
    2024-07-07
  • Spring中11個最常用的擴展點總結(jié),你知道幾個

    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ū)別說明

    這篇文章主要介紹了Maven中plugins與pluginManagement的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 關(guān)于springboot打包目錄全解析

    關(guān)于springboot打包目錄全解析

    這篇文章主要介紹了springboot打包目錄解析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 啟用Spring事務(wù)管理@EnableTransactionManagement示例解析

    啟用Spring事務(wù)管理@EnableTransactionManagement示例解析

    這篇文章主要為大家介紹了啟用Spring事務(wù)管理@EnableTransactionManagement示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • SpringCloud中的Ribbon負載均衡詳細解讀

    SpringCloud中的Ribbon負載均衡詳細解讀

    這篇文章主要介紹了SpringCloud中的Ribbon負載均衡詳細解讀,當系統(tǒng)面臨大量的用戶訪問,負載過高的時候,通常會增加服務(wù)器數(shù)量來進行橫向擴展(集群),多個服務(wù)器的負載需要均衡,以免出現(xiàn)服務(wù)器負載不均衡,部分服務(wù)器負載較大,部分服務(wù)器負載較小的情況,需要的朋友可以參考下
    2023-11-11
  • IDEA之web項目導(dǎo)入jar包方式

    IDEA之web項目導(dǎo)入jar包方式

    這篇文章主要介紹了IDEA之web項目導(dǎo)入jar包方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • SpringBoot快速構(gòu)建應(yīng)用程序方法介紹

    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()的使用

    這篇文章主要介紹了java IO流 之 輸入流 InputString()的使用,以及讀取數(shù)據(jù)的三種方式詳解,非常不錯,需要的朋友可以參考下
    2016-12-12
  • 詳解Java如何實現(xiàn)小頂堆和大頂堆

    詳解Java如何實現(xiàn)小頂堆和大頂堆

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識,文章圍繞著Java如何實現(xiàn)小頂堆和大頂堆展開,文中有非常詳細的解釋及代碼示例,需要的朋友可以參考下
    2021-06-06

最新評論

阳信县| 德化县| 瑞安市| 海阳市| 桐庐县| 城固县| 望江县| 墨竹工卡县| 涞源县| 明溪县| 霍城县| 肃南| 来安县| 皋兰县| 安吉县| 梅州市| 林芝县| 买车| 喀什市| 瓦房店市| 赣州市| 古田县| 辉南县| 桃园县| 白河县| 宜兰市| 夏津县| 容城县| 祁连县| 乐亭县| 榆林市| 周口市| 泗洪县| 兴隆县| 嘉祥县| 临沧市| 牡丹江市| 横山县| 克山县| 孟津县| 阜平县|