Spring--實體類Date欄位多種格式的校驗
阿新 • • 發佈:2019-01-23
情景說明:
採用mybatis-gen生成的bean類,資料庫中的Datetime欄位對映為Date欄位
頁面上傳來的內容是 yyyy-MM-dd 日期格式,用@Past註解日期,如下
@Past(message = "時間有誤")
//@DateTimeFormat(pattern="yyyy-MM-dd")
private Date createTime;
報錯資訊如下:
Failed to convert property value of type java.lang.String to required type java.util.Date for property createTime; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.validation.constraints.Past java.util.Date for value 2014-05-21; nested exception is java.lang.IllegalArgumentException
解決方案:應用DateTimeFormat註解,把上面註釋的程式碼取消註釋。