java-org.springframework.core.convert.ConversionFailedException- 前端傳string解析date異常
關於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;
參考文章: