> 文档中心 > Cannot deserialize value of type `java.util.Date` from String “2022-04-07 10:15:51“

Cannot deserialize value of type `java.util.Date` from String “2022-04-07 10:15:51“

出现的问题

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2022-04-07 10:15:51": not a valid representation (error: Failed toparse Date value '2022-04-07 10:15:51': Cannot parse date "2022-04-07 10:15:51": 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-07 10:15:51": not a valid representation (error: Failed to parse Date value'2022-04-07 10:15:51': Cannot parse date "2022-04-07 10:15:51": while itseems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null))<EOL> at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1,column: 40] (through reference chain: com.imust.imustcomment.entity.Comment["createtime"])]    

解决问题
出现问题的原因:请求数据格式: ‘yyyy-MM-dd HH:mm:ss’,在接收到数据的时候,需要通过jackson把数据转化。jackson转化默认的时间格式 'yyyy-MM-dd’T’HH:mm:ss.SSS’,所以就会出现异常。

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")    private Date createtime;