后台LocalDateTime参数的接收问题(Failed to convert value of type ‘java.lang.String‘ to required type ‘java.tim
后台LocalDateTime参数的接收问题(Failed to convert value of type ‘java.lang.String’ to required type ‘java.time.LocalDateTime’)
使用postman测试接口时,发现传递的时间不能够被后端获取,其中postman请求的截图如下:
controller层接口的方法为:

报错如下:

解决方法:
通过@DateTimeFormat注解,去解析前端传入的时间格式字符串然后转化成LocaDateTime的时间类型。注意:格式化的时候,格式需要和前台传入值得格式一致,并且看清楚参数类型到底是LocalDateTime还是LocalTime

顺便说一下,@JsonFormat:他和@DateTimeFormat正好对应关系,如果我们从DB中取得时间类型数据,想传回前端,并且规定格式,那么就可以使用@JsonFormat(pattern=“yyyy-MM-dd”,timezone = “GMT+8”)
pattern:是你需要转换的时间日期的格式
timezone:是时间设置为东八区,避免时间在转换中有误差
from:后台LocalDateTime参数的接收问题(Failed to convert value of type ‘java.lang.String‘ to required type ‘java.tim_java localdatetime 接收参数为0-CSDN博客