关于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;
参考文章: