Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'
首先這個錯誤的意思是 前台頁面傳遞在string類型的時間數據,比如'2020-05-31 10:00:00' 后台使用Date類型去接收,但是報錯了。
解決方法:
要解決這個問題其實很簡單, 在接收的字段上面,添加下面的注解 就可以了
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") //返回時間類型
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") //接收時間類型
private Date startTime;
注解解釋:
@DateTimeFormat
該注解自動會解析處理,會把字符串類型 按照格式yyyy-MM-dd HH:mm:ss 轉換成時間類型
@JsonFormat
這個注解是spring里面controller返回到頁面的的轉換. 把時間類型 轉換成JSON格式類型,
前提取出進行展示.