Class ObjectMapperBuilder

java.lang.Object
io.leandev.appfuse.json.ObjectMapperBuilder

public class ObjectMapperBuilder extends Object
  • Method Details

    • create

      public static ObjectMapperBuilder create()
      創建新的 ObjectMapperBuilder 實例
    • createWithDefaults

      public static ObjectMapperBuilder createWithDefaults()

      創建具有預設配置的 ObjectMapperBuilder

      Jackson 3.x 已內建 Java 8 日期時間類型支援 (LocalDate、LocalDateTime、LocalTime 等)

    • withDateFormat

      public ObjectMapperBuilder withDateFormat(String pattern, ZoneId zoneId)
      設置日期時間格式
    • withDateFormat

      public ObjectMapperBuilder withDateFormat(String pattern)
    • withZonedDateTime

      public ObjectMapperBuilder withZonedDateTime()
      添加日期時間序列化器
    • withInstant

      public ObjectMapperBuilder withInstant()
    • withDuration

      public ObjectMapperBuilder withDuration()
      添加自Duration序列化器
    • withBytes

      public ObjectMapperBuilder withBytes()
      添加Byte序列化器
    • withSerializationFeature

      public ObjectMapperBuilder withSerializationFeature(tools.jackson.databind.SerializationFeature feature, boolean state)
      添加序列化功能配置
    • withLowercaseEnums

      public ObjectMapperBuilder withLowercaseEnums()

      enum 的 wire 值一律小寫

      啟用後 OrderStatus.PENDING_CONFIRMATION 序列化為 "pending_confirmation", 反序列化則容忍大小寫("PENDING_CONFIRMATION" 亦可讀入)。enum 本身不需再逐個 標註 @JsonValue / @JsonCreator

      預設關閉:既有下游可能已將 UPPER_SNAKE 常數名直接當成 wire 值,開啟即變更 其 API 契約。遷移時機由各應用自行決定,框架不代為決定。

      不影響 DB 持久化——@Enumerated(EnumType.STRING) 仍以 name() 存大寫。

      查詢參數/路徑變數走 Spring ConversionService、不經 Jackson,需另行註冊 StringToEnumConverterFactory 才會一致。

    • withModule

      public ObjectMapperBuilder withModule(tools.jackson.databind.JacksonModule customModule)
      註冊自定義模組
    • build

      public tools.jackson.databind.ObjectMapper build()
      構建 ObjectMapper 實例
    • applyLowercaseEnums

      public static tools.jackson.databind.json.JsonMapper.Builder applyLowercaseEnums(tools.jackson.databind.json.JsonMapper.Builder builder)

      把「enum wire 值一律小寫」套用到任一 JsonMapper.Builder

      供兩個消費點共用同一規則:本 builder 的 withLowercaseEnums()(獨立 mapper), 以及 LowercaseEnumJsonMapperCustomizer(Spring Boot 自動配置的 web mapper)。

      Parameters:
      builder - 待套用的 builder
      Returns:
      同一個 builder,供串接
    • defaultObjectMapper

      public static tools.jackson.databind.ObjectMapper defaultObjectMapper()
      快速創建具有預設配置的 ObjectMapper