天天看点

SpringBoot中的Jackson中日期反序列化问题

在页面中日期格式的参数,放到以字符串方式(yyyy-MM-dd HH:mm:ss)用json格式submit的时候,报错如下:

org.springframework.http.converter.HttpMessageNotReadableException: 
	JSON parse error: Can not deserialize value of type java.util.Date from String "2022-03-17 18:43:02": not a valid representation 
           

SpringBoot中默认JSON转换是使用Jackson,Jackson支持如下几种日期格式:

yyyy-MM-dd'T'HH:mm:ss.SSSZ
    yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
           

在model对象上增加注释,指定格式即可:

@JsonFormat( pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date createTime;