关于SSM框架前后端string转date的lang异常及处理方法

本文介绍了解决前端传来的字符串在后台接口中自动注入并转换为Date类型时出现的错误的方法。通过在接口中使用Spring框架的@InitBinder注解,注册自定义的日期编辑器,可以成功实现字符串到Date类型的转换。

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

前端传来的string在后台接口中自动注入并转换Date时如果报failed convert type of java.lang.String to required type of java.util.Date时,说明在注入属性前初始化转换这两种类型没做或者没做好此时怎么办呢?

在接口中加一个在注入对象前的一个初始化即可很好用:

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import java.text.SimpleDateFormat;
import org.springframework.beans.propertyeditors.CustomDateEditor;

@InitBinder
	protected void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值