溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot中Jackson日期格式化的方法

發布時間:2022-04-14 10:52:14 來源:億速云 閱讀:371 作者:iii 欄目:開發技術

這篇“SpringBoot中Jackson日期格式化的方法”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“SpringBoot中Jackson日期格式化的方法”文章吧。

Jackson 日期格式化技巧

使用 Spring Boot 時,需要使用 Jackson 處理一些 Java Time API 類型的 JSON 序列化問題,在處理一些類的字段時,可以通過直接在屬性上加注解的方式來指定其格式化樣式。但是,昨天同事遇到一個格式化 Map 數據的問題,這樣就不能通過加注解來解決格式化樣式的問題了。

在網上各種搜索,各種嘗試后,終于解決了這個問題,記錄一下,以備不時之需。

閑言少敘,直接上代碼:

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 href="https://www.diguage.com/" rel="external nofollow"  rel="external nofollow"  target="_blank" >https://www.diguage.com</a>
 */
@Configuration
public class Config {
    /</strong>
     * 創建 ObjectMapper 對象,配置日期格式化
     *
     * @author D瓜哥 · <a href="https://www.diguage.com/" 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;
    }
}

后續問題

不知道通過這種方式指定日期格式化樣式后,在處理一些打格式化樣式注解的字段時,會有什么樣的表現?有機會測試一下。

補充:Jackson 統一配置 日期轉換格式

方式一:配置文件yml中配置

spring:
    jackson:
        default-property-inclusion: ALWAYS
        time-zone: GMT+8
        date-format: yyyy-MM-dd HH:mm:ss

這樣序列化后,Date類型會被格式化成配置中的格式。

方式二:配置類中配置創建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);
            }
        };
    }
}

以上就是關于“SpringBoot中Jackson日期格式化的方法”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女