【spring boot】傳遞時間類型的字符串,報錯:Failed to convert from type [java.lang.String] to type [java.util.Date]

spring boot 前臺GET請求,傳遞時間類型的字符串,後臺無法解析,報錯:Failed to convert from type [java.lang.String] to type [java.util.Date]

而POST請求,傳入時間類型字符串,後臺是可以解析成Date類型的。

出現這個錯誤,在需要接受Date類型的字符串參數的controller層中,加入:

@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

重啓,再次進行訪問,即可GET請求傳入時間字符串,後臺解析爲Date類型成功。

更多:
https://www.cnblogs.com/sxdcgaq8080/p/10399458.html
https://blog.csdn.net/gwd1154978352/article/details/75041733

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章