springboot前端传参date类型后台处理方式
生活随笔
收集整理的這篇文章主要介紹了
springboot前端传参date类型后台处理方式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
springboot前端傳參date類型后臺處理方式
先說結論:建議大家直接使用@JsonFormat,原因如下:
1、針對json格式:在配置文件中加以下配置
spring.jackson.date-format=yyyy-MM-dd HH:mm:ssspring.jackson.time-zone=GMT+82、針對form表單格式,加下面這句配置就可以
spring.mvc.dateFormat = yyyy-MM-dd HH:mm:ss3、也可以在pojo中對特定的date類型屬性加了以下配置
@DateTimeFormat來控制入?yún)?#xff0c;@JsonFormat來控制出參@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")4、前端以字符串的形式給后臺傳遞帶有格式的 日期 和 數(shù)字 數(shù)據(jù),導致后臺無法解析數(shù)據(jù):
解決方法:
總結:
1.如果前后端傳的數(shù)據(jù)都是json格式,那么后臺接數(shù)據(jù),傳數(shù)據(jù)都可以用@JsonFormat ;
2.@DateTimeFormat適合后端接收前端傳來的數(shù)據(jù),不管是不是json格式都可以正確轉(zhuǎn)換成Date型數(shù)據(jù),只要前端傳來的格式正確且后端@DateTimeFormat的pattern寫正確。但是,這個注解無法將Date型數(shù)據(jù)用json傳到前端去
綜上所述,建議大家直接使用@JsonFormat
Springboot的bean實體類接收Date類型
@GetMapping("/test")public ResponseResult docList(QueryListParam queryListParam) {return testService.queryList(queryListParam);} @Data public class QueryListParam implements Serializable {/*** 姓名*/private String name;/*** 起始時間(接收不到,會報錯)*/private Date startTime;/*** 結束時間(接收不到,會報錯)*/private Date endTime; }GET請求直接傳遞 startTime=2020-01-01 01:10:00 是接收不到了,后臺會報錯,需要在bean中Date類型的屬性上添加注解@DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”)
該注解是org.springframework.format.annotation下的
但是不用這種方法,用String進行接收,在用SimpleDateFormat進行轉(zhuǎn)換。
總結
以上是生活随笔為你收集整理的springboot前端传参date类型后台处理方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习实践:TensorFlow2 多
- 下一篇: ssm前端时间格式转换