Field error in object 'user' on field 'birthday': rejected value [1953-11-12]; codes [typeMismatch.user.birthday,typeMismatch.birthday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.birthday,birthday]; arguments []; default message [birthday]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birthday'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '1953-11-12'; nested exception is java.lang.IllegalArgumentException]]
上面的错误很明显是数据格式问题,经过查阅资料得知是springboot的日期格式默认是dd/mm/yyyy倒序格式,而我输入的是yyyy-mm-dd的中文习惯格式,所以这个转换错误。
解决办法:
在配置文件application.properties中添加一段代码从新规范日期格式就可以了:
spring.mvc.date-format=yyyy-MM-dd