content: package cn.datax.common.config;
  
  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;
  
  @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数据不返回
          objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
          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);
          // 不使用默认的dateTime进行序列化,
          objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
          // 数据精度问题
          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);
  
          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;
      }
  }
  