Failed to convert value of type 'java.lang.String' to required type 'java.util.Date

本文探讨了在Spring MVC框架中,Controller层处理JSP页面请求时遇到的参数类型不匹配异常,具体为String类型参数被误指定为Date类型接收的问题。通过添加@DateTimeFormat注解,成功解决了日期参数的转换异常,确保了系统的正常运行。

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

异常原因:Controller层获取JSP页面请求参数是String类型,而在Controller层接收时使用Date类型,属于方法参数类型不匹配异常
   警告: Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2019-06-18'; nested exception is java.lang.IllegalArgumentException
错误代码
 @RequestMapping("/test2")
    public void test2( String name, int age, Date birth){
        System.out.println(name);
        System.out.println(age);
        System.out.println(birth);
    }
解决办法:Date类型前加注解:@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
 @RequestMapping("/test2")
    public void test2( String name, int age,@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date birth){
        System.out.println(name);
        System.out.println(age);
        System.out.println(birth);
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值