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

异常原因: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);
    }

你可能感兴趣的:(异常)