【spring boot】spring boot 前台GET請求,傳遞時間類型的字符串,后台無法解析,報錯: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類型成功。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM