content: package com.clothrent.config.date;
  
  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.beans.factory.annotation.Value;
  import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  
  import java.time.LocalDate;
  import java.time.LocalDateTime;
  import java.time.format.DateTimeFormatter;
  
  
  @Configuration
  public class LocalDateTimeSerializerConfig {
  
      @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
      private String pattern;
  
      // localDateTime 序列化器
      @Bean
      public LocalDateTimeSerializer localDateTimeSerializer() {
          return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
      }
      // localDateTime 反序列化器
      @Bean
      public LocalDateTimeDeserializer localDateTimeDeserializer() {
          return new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(pattern));
      }
  
      // localDate 序列化器
      @Bean
      public LocalDateSerializer localDateSerializer() {
          return new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
      }
      // localDate 反序列化器
      @Bean
      public LocalDateDeserializer localDateDeserializer() {
          return new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
      }
  
      @Bean
      public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
  //        return new Jackson2ObjectMapperBuilderCustomizer() {
  //            @Override
  //            public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
  ////                jacksonObjectMapperBuilder.featuresToDisable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
  //                jacksonObjectMapperBuilder.serializerByType(LocalDateTime.class, localDateTimeSerializer());
  //                jacksonObjectMapperBuilder.deserializerByType(LocalDateTime.class,localDateTimeDeserializer());
  //            }
  //        };
          //这种方式同上
          return builder -> {
              builder.serializerByType(LocalDateTime.class, localDateTimeSerializer());
              builder.serializerByType(LocalDate.class,localDateSerializer());
              builder.deserializerByType(LocalDate.class,localDateDeserializer());
              builder.deserializerByType(LocalDateTime.class,localDateTimeDeserializer());
              builder.simpleDateFormat(pattern);
          };
      }
  
  }