错误代码:
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”,但解析失败(宽容度?空))
原因:
@TableField(value = "date")
@ApiModelProperty(value = "时间")
private Date date;
因为在bean实习类中,时间字段使用数据类型 Date ,在测试时候传入参数,格式错误!
方法:
使用注解: @JsonFormat
依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
写法:
@TableField(value = "date")
@ApiModelProperty(value = "时间")
@JsonFormat(locale="zh", timezone="GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date date;
注解@DateTimeFormat
依赖:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
-
注解@JsonFormat主要是后台到前台的时间格式的转换
-
注解@DataFormat主要是前台到后台的时间格式的转换