乐趣区

关于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;
退出移动版