content: package com.newone.mall.common.config;
  
  import com.fasterxml.jackson.annotation.JsonInclude;
  import com.fasterxml.jackson.databind.ObjectMapper;
  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 org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  
  import java.time.LocalDate;
  import java.time.LocalDateTime;
  import java.time.LocalTime;
  import java.time.format.DateTimeFormatter;
  import java.util.TimeZone;
  
  /**
   * @author newone
   * @date 2023/11/14
   * @description: 自定义 Jackson
   */
  @Configuration
  public class JacksonConfig {
  
      @Bean
      public ObjectMapper objectMapper() {
          // 初始化一个 ObjectMapper 对象，用于自定义 Jackson 的行为
          ObjectMapper objectMapper = new ObjectMapper();
          // JavaTimeModule 用于指定序列化和反序列化规则
          JavaTimeModule javaTimeModule = new JavaTimeModule();
  
          // 支持 LocalDateTime、LocalDate、LocalTime
          javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
          javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
          javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
          javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
          javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
          javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
  
          objectMapper.registerModule(javaTimeModule);
  
          // 设置时区
          objectMapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
  
          // 设置凡是为 null 的字段，返参中均不返回，请根据项目组约定是否开启
          // objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
  
          return objectMapper;
      }
  }