springboot 前后端日期json格式化

勘正

1、常规代码

@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;//创建时间

2、字段类型是LocalDateTime,可以选择手动设置序列化与反序列化的类

@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)

3、前端 -> 后端。
当前端传来的是键值对,用@DateTimeFormat 规定接收的时间格式。
当前端传来json串,后台用@ReuqestBody接收,用@JsonFormat 规定接收的时间格式。

4、后端 -> 前端。
后端返回给前端的时间值,只能用@JsonFormat 规定返回格式,@DateTimeFormat 无法决定返回值的格式。

备注:

@JsonFormat 是jackson提供。
@DateTimeFormat 由spring提供。

posted @ 2021-01-06 11:24  我是属车的  阅读(1902)  评论(0编辑  收藏  举报