JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime

爱被打了一巴掌 2023-02-11 07:16 143阅读 0赞

方案一

  1. @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
  2. @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
  3. private LocalDateTime gmtCreate;
  4. @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
  5. private LocalDateTime gmtModified;

方案二

LocalDateTime直接改称Date

  1. private Date gmtCreate;
  2. private Date gmtModified;

方案三

pom.xml添加依赖

  1. <dependency>
  2. <groupId>com.fasterxml.jackson.datatype</groupId>
  3. <artifactId>jackson-datatype-jsr353</artifactId>
  4. <version>2.11.0</version>
  5. <type>bundle</type>
  6. </dependency>

添加application.yml自定义配置项目

  1. spring:
  2. jackson:
  3. local-date-time-format: yyyy-MM-dd HH:mm:ss
  4. local-date-format: yyyy-MM-dd
  5. local-time-format: HH:mm:ss

添加jackson配置类

  1. package com.example.demo.config;
  2. import java.time.LocalDate;
  3. import java.time.LocalDateTime;
  4. import java.time.LocalTime;
  5. import java.time.format.DateTimeFormatter;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import com.fasterxml.jackson.databind.ObjectMapper;
  10. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
  11. import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
  12. import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
  13. import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
  14. import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
  15. import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
  16. import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
  17. @Configuration
  18. public class JacksonConfig {
  19. @Value("${spring.jackson.local-date-time-format:yyyy-MM-dd HH:mm:ss}")
  20. String localDateTimeFormat;
  21. @Value("${spring.jackson.local-date-format:yyyy-MM-dd}")
  22. String localDateFormat;
  23. @Value("${spring.jackson.local-time-format:HH:mm:ss}")
  24. String localTimeFormat;
  25. @Bean
  26. public ObjectMapper objectMapper() {
  27. ObjectMapper om = new ObjectMapper();
  28. JavaTimeModule javaTimeModule = new JavaTimeModule();
  29. javaTimeModule.addSerializer(LocalDateTime.class,
  30. new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(localDateTimeFormat)));
  31. javaTimeModule.addSerializer(LocalDate.class,
  32. new LocalDateSerializer(DateTimeFormatter.ofPattern(localDateFormat)));
  33. javaTimeModule.addSerializer(LocalTime.class,
  34. new LocalTimeSerializer(DateTimeFormatter.ofPattern(localTimeFormat)));
  35. javaTimeModule.addDeserializer(LocalDateTime.class,
  36. new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(localDateTimeFormat)));
  37. javaTimeModule.addDeserializer(LocalDate.class,
  38. new LocalDateDeserializer(DateTimeFormatter.ofPattern(localDateFormat)));
  39. javaTimeModule.addDeserializer(LocalTime.class,
  40. new LocalTimeDeserializer(DateTimeFormatter.ofPattern(localTimeFormat)));
  41. om.registerModule(javaTimeModule);
  42. return om;
  43. }
  44. }

发表评论

表情:
评论列表 (有 0 条评论,143人围观)

还没有评论,来说两句吧...

相关阅读