出现这个错误Field error in object ‘employee’ on field ‘birth’: rejected value [2017-11-11];
你可以尝试这样解决
当后台出现这个错误
Field error in object 'employee' on field 'birth': rejected value [2017-11-11]; codes [typeMismatch.employee.birth,typeMismatch.birth,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.birth,birth]; arguments []; default message [birth]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birth'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2017-11-11'; nested exception is java.lang.IllegalArgumentException]]
前端出现这个页面
导致这个原因的就是 前端默认输入的日期格式::yyyy/MM/dd
而我输入的格式却是: yyyy-MM-dd
最简单的方法 就是 改变一下格式
下面就是源代码
在IDEA中 键盘按下ctrl + shift + n:查找下面文件WebMvcAutoConfiguration
找到下面的代码方法点击进入,ctrl+左点击getDate()
你将会看到下面代码
ctrl+左点击date
默认的格式就是斜线的格式
你可以在springboot的配置文件 添加配置文件 并且在该实体类日期属性添加数据类型格式的注解
配置文件
spring:
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd
在实体类属性添加数据类型格式的注解
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birth;
这样就修改完成了
仅作为参考 自学 也欢迎各位指正