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

2022-07-31,,,

运行:

完整的报错信息是:
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]]


当我在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、在实体类添加时间的注解

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

再运行测试输出成功:

还有好多方法可以解决这个问题的,若是你有更好的方法也可以在下方留言告诉我

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

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

下载本文的Word格式文档,以方便收藏与打印。