关于java:spring-boot-传Date类型数据报错无法读取JSON无法从String构造javautilDate实例

前端传值:

{
    "province":"省份",
    "publish":"2021-10-01 00:00:00"
}

Entity:

    @ApiModelProperty(value = "推出工夫")
    private Date publish;

报错信息如下:

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2021-10-01 00:00:00": not a valid representation (error: Failed to parse Date value '2021-10-01 00:00:00': Cannot parse date "2021-10-01 00:00:00": 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 "2021-10-01 00:00:00": not a valid representation (error: Failed to parse Date value '2021-10-01 00:00:00': Cannot parse date "2021-10-01 00:00:00": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', 
parsing fails (leniency? null))<LF> at [Source: (PushbackInputStream); line: 3, column: 15] (through reference chain: houseHistory.entity.House["publish"])]

报错内容解释:

 无奈读取JSON:无奈从String结构java.util.Date实例
 value'2012-07-21 12:11:12':有效示意("yyyy-MM-dd'T'HH:mm:ss.SSSZ","yyyy-MM-dd'T'HH:mm:ss.SSS'Z'","EEE,dd MMM yyyy HH:mm :ss zzz","yyyy-MM-dd"))

解决办法如下:
在实体Date类型的字段上应用@JsonFormat注解格式化日期,如下

    @ApiModelProperty(value = "推出工夫")
    @JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
    private Date publish;

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理