欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解决Field error in object ‘tbPet‘ on field ‘birthday‘: rejected value [2020-07-30]; codes [typeMismatch.

程序员文章站 2022-03-26 16:33:48
运行:完整的报错信息是:Field error in object ‘tbPet’ on field ‘birthday’: rejected value [2020-07-30]; codes [typeMismatch.tbPet.birthday,typeMismatch.birthday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessa...

运行:
解决Field error in object ‘tbPet‘ on field ‘birthday‘: rejected value [2020-07-30]; codes [typeMismatch.
解决Field error in object ‘tbPet‘ on field ‘birthday‘: rejected value [2020-07-30]; codes [typeMismatch.

解决Field error in object ‘tbPet‘ on field ‘birthday‘: rejected value [2020-07-30]; codes [typeMismatch.

完整的报错信息是:
Field error in object ‘tbPet’ on field ‘birthday’: rejected value [2020-07-30]; codes [typeMismatch.tbPet.birthday,typeMismatch.birthday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [tbPet.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 ‘2020-07-30’; nested exception is java.lang.IllegalArgumentException]]

解决Field error in object ‘tbPet‘ on field ‘birthday‘: rejected value [2020-07-30]; codes [typeMismatch.
当我在jsp页面接收date信息,回到controller输出的时候出错了,这是说白了是java.lang.String和java.util.Date之间的问题

解决方法共两步:
1、首先需要在maven文件引入joda-time依赖

 <!--获取时间的架包--> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.3</version> </dependency> 

2、在实体类添加时间的注解
解决Field error in object ‘tbPet‘ on field ‘birthday‘: rejected value [2020-07-30]; codes [typeMismatch.

 @DateTimeFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8") 

再运行测试输出成功:
解决Field error in object ‘tbPet‘ on field ‘birthday‘: rejected value [2020-07-30]; codes [typeMismatch.
还有好多方法可以解决这个问题的,若是你有更好的方法也可以在下方留言告诉我

本文地址:https://blog.csdn.net/qq_44739706/article/details/107889893

相关标签: SpringMVC