前端传值:

{
    "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;

Kason
209 声望6 粉丝