当前位置:   article > 正文

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

json parse error: cannot deserialize value of type `java.time.localdatetime`

方案一

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
  1. @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
  2. private LocalDateTime gmtCreate;
  3. @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
  4. 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. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/813797
推荐阅读
相关标签
  

闽ICP备14008679号