background

  • I don’t know if you have encountered in your work and study, a new SpringBoot project, the operation of add, delete, change, check services and input and output parameters contain LocalDateTime serialization and deserialization problems. For example, an error is reported when receiving arguments of time type: Failed to convert from type [java.lang.String] to type [java.time.LocalDateTime] for value ‘2020-12-12 00:00:00’; The LocalDateTime format returned to the front-end child is an array format, leaving the front-end scratching its head. These are problems caused by improper serialization and deserialization of LocalDateTime.

  • Serialization: The process of converting Java objects into byte sequences.

  • Deserialization: The process of restoring a sequence of bytes to Java objects.

The solution

  • Deserialization of url LocalDateTime parameter in K-V style: add to parameter field
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
Copy the code
  • Deserialization of URL arguments (global) :
@ControllerAdvice public class ExceptionResolver { @InitBinder public void initBinder(WebDataBinder webDataBinder) { webDataBinder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(LocalDate.parse(text, DateTimeFormatter.ISO_DATE)); }}); webDataBinder.registerCustomEditor(LocalDateTime.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(LocalDateTime.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); }}); webDataBinder.registerCustomEditor(LocalTime.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(LocalTime.parse(text, DateTimeFormatter.ISO_TIME)); }}); }}Copy the code
  • Json input parameter in body: added to the parameter field
    @JsonFormat(shape = JsonFormat.Shape.STRING,pattern = "yyyy-MM-dd HH:mm:ss")
Copy the code
  • Json input parameter in body (global resolver) : The serialization code takes care of the formatting problem. The code is placed in a class that begins with @confuguration
@Bean(name = "mapperObject") public ObjectMapper getObjectMapper() { ObjectMapper om = new ObjectMapper(); JavaTimeModule javaTimeModule = new JavaTimeModule(); // serialize, Write to the output stream 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"))); // Reverse sequence, read the input stream, Can only be deserialized into the Body of the json data format. Oh 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"))); om.registerModule(javaTimeModule); return om; }Copy the code