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

如何配置Spring Boot中的Jackson序列化

 更新時間:2025年04月12日 09:52:19   作者:LOVE_DDZ  
在開發(fā)基于Spring Boot的應(yīng)用程序時,Jackson是默認(rèn)的JSON序列化和反序列化工具,本文將詳細(xì)介紹如何在Spring Boot中配置Jackson,以滿足這些需求,感興趣的朋友一起看看吧

配置Spring Boot中的Jackson序列化

在開發(fā)基于Spring Boot的應(yīng)用程序時,Jackson是默認(rèn)的JSON序列化和反序列化工具。它提供了強大的功能,可以靈活地處理JSON數(shù)據(jù)。然而,Jackson的默認(rèn)行為可能無法完全滿足我們的需求。例如,日期格式、空值處理、數(shù)據(jù)精度等問題可能需要自定義配置。本文將詳細(xì)介紹如何在Spring Boot中配置Jackson,以滿足這些需求。

1. 為什么需要自定義Jackson配置?

Jackson的默認(rèn)行為在大多數(shù)情況下是合理的,但在實際開發(fā)中,我們可能需要對以下方面進(jìn)行自定義:

  • 日期格式:默認(rèn)情況下,Jackson會將日期序列化為時間戳,這可能不符合我們的需求。
  • 空值處理:默認(rèn)情況下,Jackson會忽略空值,但我們可能需要保留空值。
  • 數(shù)據(jù)精度:對于BigDecimalBigInteger等類型,直接序列化可能會導(dǎo)致精度問題。
  • 自定義序列化:對于某些復(fù)雜類型,我們可能需要自定義序列化邏輯。

2. 配置JacksonConfig

在Spring Boot中,可以通過創(chuàng)建一個@Configuration類并定義一個ObjectMapper的Bean來自定義Jackson的行為。

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
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.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;
/**
 * @author XiaoXin
 */
@Configuration
public class JacksonConfig {
    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ObjectMapper objectMapper = builder.createXmlMapper(false)
                .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                .featuresToDisable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
                .timeZone(TimeZone.getTimeZone("Asia/Shanghai"))
                .build();
        // null數(shù)據(jù)返回
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 反序列化時候遇到不匹配的屬性并不拋出異常
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 序列化時候遇到空對象不拋出異常
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        // 反序列化的時候如果是無效子類型,不拋出異常
        objectMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
        // 不使用默認(rèn)的dateTime進(jìn)行序列化,
        objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
        // 數(shù)據(jù)精度問題
        SimpleModule simpleModule = new SimpleModule()
                .addSerializer(Long.class, ToStringSerializer.instance)
                .addSerializer(Long.TYPE, ToStringSerializer.instance)
                .addSerializer(BigInteger.class, ToStringSerializer.instance)
                .addSerializer(BigDecimal.class, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        // 配置Java 8時間日期模塊
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        objectMapper.registerModule(javaTimeModule).registerModule(new ParameterNamesModule());
        return objectMapper;
    }
}

到此這篇關(guān)于配置Spring Boot中的Jackson序列化的文章就介紹到這了,更多相關(guān)Spring Boot Jackson序列化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

屯门区| 伊吾县| 武夷山市| 桐梓县| 明星| 得荣县| 苏尼特左旗| 昌宁县| 上杭县| 郸城县| 灌南县| 兖州市| 新密市| 兰西县| 房产| 许昌市| 武冈市| 南雄市| 澄江县| 六枝特区| 井研县| 中江县| 江都市| 玉林市| 莱阳市| 昭觉县| 隆子县| 梅河口市| 寻甸| 荆门市| 周至县| 四子王旗| 合山市| 若尔盖县| 遵化市| 舟山市| 大埔县| 长垣县| 眉山市| 大埔县| 徐闻县|