绑定报错:Failed to convert from type [java.lang.String] to type [java.util.Date] for value ‘2020-09-12‘;

在使用SSM框架时遇到将String类型'2020-09-12'转换为Date类型的错误。报错信息表明绑定过程中发生了类型不匹配。解决方法是在实体类中的Date字段上添加注解进行日期格式化。如果热部署不生效,建议重启项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用SSM框架,页面传输数据时,日期有绑定报错
是因为String类型无法转换为Date类型
以下是我的报错信息:

2020-09-21 16:05:31.079  WARN 6068 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'user' on field 'birth': rejected value [2020-09-12]; codes [typeMismatch.user.birth,typeMismatch.birth,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.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 '2020-09-12'; nested exception is java.lang.IllegalArgumentException]]

解决的方法就是:
在实体类中,给Date日期类型添加注解进行格式化
如下:

@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birth;

若热部署刷新不行时,重新启动项目

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值