当前位置:   article > 正文

【错误】日期转换异常 JSONorg.springframework.http.converter.HttpMessageNotReadableException: JSON parse error_cannot parse date "2023-12-15 13:58:03": while it

cannot parse date "2023-12-15 13:58:03": while it seems to fit format 'yyyy-

错误代码:

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2022-04-13 13:07:43": not a valid representation (error: Failed to parse Date value '2022-04-13 13:07:43': Cannot parse date "2022-04-13 13:07:43": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2022-04-13 13:07:43": not a valid representation (error: Failed to parse Date value '2022-04-13 13:07:43': Cannot parse date "2022-04-13 13:07:43": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null))

翻译:

org.springframework.http.converter.HttpMessageNotReadableException:JSON解析错误:无法反序列化字符串“2022-04-13 13:07:43”中类型为“java.util.Date”的值:不是有效的表示形式(错误:未能解析日期值“2022-04-23 13:07:33”:无法解析日期“2022-04-33 13:07:53”:虽然它似乎符合格式“yyyy-MM-dd”“HH:MM:ss.SSX”,但解析失败(宽大?空);嵌套异常为com.fasterxml.jackson.databind.exc。InvalidFormatException:无法反序列化字符串“2022-04-13 13:07:43”中类型为“java.util.Date”的值:不是有效的表示形式(错误:无法分析日期值“2022-04-23 13:07:33”:无法解析日期“2022-04-33 13:07:53”:虽然它似乎符合格式“yyyy-MM-dd”“HH:MM:ss.SSX”,但解析失败(宽容度?空))

原因:

  1. @TableField(value = "date")
  2. @ApiModelProperty(value = "时间")
  3. private Date date;

因为在bean实习类中,时间字段使用数据类型 Date ,在测试时候传入参数,格式错误!

方法:

使用注解: @JsonFormat

依赖:

  1. <dependency>
  2. <groupId>com.fasterxml.jackson.core</groupId>
  3. <artifactId>jackson-annotations</artifactId>
  4. <version>2.8.8</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.fasterxml.jackson.core</groupId>
  8. <artifactId>jackson-databind</artifactId>
  9. <version>2.8.8</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.codehaus.jackson</groupId>
  13. <artifactId>jackson-mapper-asl</artifactId>
  14. <version>1.9.13</version>
  15. </dependency>

写法:

  1. @TableField(value = "date")
  2. @ApiModelProperty(value = "时间")
  3. @JsonFormat(locale="zh", timezone="GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
  4. private Date date;

注解@DateTimeFormat

依赖:

  1. <dependency>
  2. <groupId>joda-time</groupId>
  3. <artifactId>joda-time</artifactId>
  4. <version>2.3</version>
  5. </dependency>
  • 注解@JsonFormat主要是后台到前台的时间格式的转换

  • 注解@DataFormat主要是前台到后台的时间格式的转换

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

闽ICP备14008679号