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提供。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM