Using mybatis plus 3.1.2 and Druid 1.1.20 invalidates the serialization and deserialization of localdateformat to a custom date format string

Keywords: Programming Druid Mybatis JSON Attribute

Time record

April 21, 2020

Preface

      At present, the project reconstruction adopts springboot2.1.13 to integrate mybatis-plus3.1.2 and druid connection pool 1.1.20. When the response body json result set and the request body json result set are returned in the controller controller controller, both serialization and deserialization are invalid. At the beginning, the failure is caused by no relevant configuration or adding @ JsonFormat annotation. Therefore, the following is adopted There are two ways to configure:

Mode 1 (configure @ JsonFormat annotation on entity attribute with LocalDateTime type, and give timezone and pattern attributes):

   

Method 2 (according to the online tutorial, create the LocalDateTimeFormatConfig configuration class and configure the formatter of LocalDate, LocalTime and LocalDateTime, or configure the mappingjackson 2httpmessageconverter in the WebMvcConfigurer configuration class):


/**
 * Receive time configuration bean
 */
/*

@Configuration
public class LocalDateTimeFormatConfig {

  @Bean(name = "mapperObject")
  public ObjectMapper getObjectMapper() {
    ObjectMapper om = new ObjectMapper();
    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")));
    om.registerModule(javaTimeModule);
    return om;
  }

  @Bean
  public Formatter<LocalDate> localDateFormatter() {
    return new Formatter<LocalDate>() {
      @Override
      public String print(LocalDate object, Locale locale) {
        return object.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
      }

      @Override
      public LocalDate parse(String text, Locale locale) throws ParseException {
        return LocalDate.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
      }
    };
  }

  @Bean
  public Formatter<LocalDateTime> localDateTimeFormatter() {
    return new Formatter<LocalDateTime>() {
      @Override
      public String print(LocalDateTime object, Locale locale) {
        return object.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
      }

      @Override
      public LocalDateTime parse(String text, Locale locale) throws ParseException {
        return LocalDateTime.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
      }
    };
  }

  @Bean
  public Formatter<LocalTime> localTimeFormatter() {
    return new Formatter<LocalTime>() {
      @Override
      public String print(LocalTime object, Locale locale) {
        return object.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
      }

      @Override
      public LocalTime parse(String text, Locale locale) throws ParseException {
        return LocalTime.parse(text, DateTimeFormatter.ofPattern("HH:mm:ss"));
      }
    };
  }

}
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {

  @Bean
  public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper objectMapper = new ObjectMapper();
    // Do not throw an error when converting json to an empty object
    objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    // Disable encountered unknown property throw exception
    objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    // Do not use scientific count output when serializing BigDecimal
    objectMapper.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
    // Date and time formatting
    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);
    jsonConverter.setObjectMapper(objectMapper);
    return jsonConverter;
  }

@Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(customJackson2HttpMessageConverter());
  }
}

However, both serialization and deserialization fail in the above methods. The response body results:

Solution steps:

1. Find the Gihub project on the official website of mybatis plus and druid. Project path: https://github.com/alibaba/druid/releases , related introduction:

Upgrade druid version to 1.20 or above.

2. Downgrade the version of mybatis plus jar to the version below 3.1.2

3. The entity class does not use the type of LocalDateTime but is replaced by the type of Date

Which of the above methods will be adopted depends on the current situation of the project and will be handled preferentially.

Problems found:

        If you adopt the first or second method in the above solution, then add @ JsonFormat annotation in entity class or create LocalDateTimeFormatConfig configuration class, and configure formatter of LocalDate, LocalTime and LocalDateTime, or configure mappingjackson 2httpmessageconverter in WebMvcConfigurer configuration class If FastJsonHttpMessageConverter is configured in the R configuration class, the serialization failure will occur again. The reasons for this can be found in these two articles: https://www.cnblogs.com/page12/p/8166935.html,https://www.cnblogs.com/page12/p/8168107.html . It's explained why MappingJackson2HttpMessageConverter and FastJsonHttpMessageConverter are added to the HttpMessageConverter collection at the same time, but the only effect is FastJsonHttpMessageConverter.

Posted by dnice on Mon, 20 Apr 2020 21:29:42 -0700