报错如下:
HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type java.util.Date
from String "2023/3/21 23:35:36": expected format "yyyy-MM-dd HH:mm:ss"; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException
前端是通过下面的代码获取时间的:
const now = new Date();
this.Form.mtime = now.toLocaleString();
后端放了这个:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date mtime;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
private Date mtime;
也可以在前端修改代码,将时间格式化为符合后端期望的格式,例如:
const now = new Date();
this.Form.mtime = now.toISOString().slice(0, 19).replace('T', ' ');
其中 toISOString() 方法返回符合 ISO 标准的时间格式,需要将其截取前 19 个字符(去除毫秒)并将 "T" 替换为空格,即可得到符合期望格式的时间。