问题:
在使用LocalDateTime 的时候, 获取到的时间格式是一个json串, 这样不利于前端对接. 接口数据格式如下:
{
略...
"name":"zhangsan"
"createTime": {
"month": "DECEMBER",
"year": 2020,
"dayOfMonth": 24,
"hour": 15,
"minute": 5,
"monthValue": 12,
"nano": 0,
"second": 12,
"dayOfWeek": "THURSDAY",
"dayOfYear": 359,
"chronology": {
"id": "ISO",
"calendarType": "iso8601"
}
},
略...
}
伪代码:
public class Demo{
private String name;
private LocalDateTime updateTime;
}
解决办法:
在LocalDateTime上加两行注解
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
public class Demo{
private String name;
// 主要加这两行注解
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
private LocalDateTime updateTime;
}