關於SpringMVC前台日期作為實體類對象參數類型轉換錯誤解決
異常資訊:
Field error in object 'tblHouse' on field 'houseTime': rejected value [2018-01-26]; codes [typeMismatch.tblHouse.houseTime,typeMismatch.houseTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [tblHouse.houseTime,houseTime]; arguments []; default message [houseTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'houseTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.baomidou.mybatisplus.annotations.TableField @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2018-01-26'; nested exception is java.lang.IllegalArgumentException: Invalid format: "2018-01-26" is too short]
異常情境描述
前端輸入框為選擇日期的外掛程式,選擇日期完成,請求後台添加資料,報錯。 解決方式
在實體類中的屬性上添加該註解,即可解決該錯誤
@DateTimeFormat(pattern = "yyyy-MM-dd") private Date houseTime;
參考文章:
http://www.linuxidc.com/Linux/2017-06/145204.htm