首頁  >  文章  >  Java  >  SpringBoot中Jackson日期格式化的方法

SpringBoot中Jackson日期格式化的方法

王林
王林轉載
2023-05-20 11:46:541202瀏覽

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日期格式化的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除