当前位置:   article > 正文

[已解决]JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String “12-21_error 11736

error 11736

时间选择器选择时间,传入后端报错

报错信息:

  1. 2023-12-21 13:15:27.683  WARN 11736 --- [nio-8090-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver
  2. : Resolved [org.springframework.http.converter.HttpMessageNotReadableException
  3. : JSON parse error
  4. : Cannot deserialize value of type `java.time.LocalDateTime` from String "12-21 13:14"
  5. : Failed to deserialize java.time.LocalDateTime:
  6. (java.time.format.DateTimeParseException) Text '12-21 13:14' could not be parsed at index 0;
  7. nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException
  8. : Cannot deserialize value of type `java.time.LocalDateTime` from String "12-21 13:14"
  9. : Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '12-21 13:14' could not be parsed at index 0<EOL> at
  10. [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 52] (through reference chain: com.example.memo.entity.Todo["time"])]

报错原因:前端使用时间选择器,只传入了月、日、时、分,与后端LocalDateTime要求格式不一样,LocalDateTime必须按"yyyy-MM-dd HH:mm:ss"格式

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime time; 

解决方法:时间选择器年、月、日、时、分,秒全部传入至后端

  1. data() {
  2. return {
  3. //时间选择器
  4. time:'',
  5. show:false,
  6. params: {
  7. year: true,
  8. month: true,
  9. day: true,
  10. hour: true,
  11. minute: true,
  12. second: true
  13. },
  14. }
  15. },
  16. //..method
  17. //用于返回选择器选择的数据。
  18. confirmtime(e){
  19. console.log(e);
  20. //此处实现选择器值的接收
  21. this.time = e.year + '-' + e.month + '-'+
  22. e.day+' '+ e.hour + ':'+ e.minute + ':' + e.second;
  23. console.log(this.time);
  24. },

PS:有大佬知道如何解决只传入月、日、时、分吗?知道的大佬可以在评论区留言

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

闽ICP备14008679号