欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringMVC自定义日期转换器不起作用,报400错误,Failed to convert value of type xxx to required type xxx

程序员文章站 2022-07-06 15:58:44
错误描述控制台:WARN efaultHandlerExceptionResolver - Resolved [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.springf...

错误描述

控制台:

WARN efaultHandlerExceptionResolver - Resolved [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 ‘2020-10-01’; nested exception is java.lang.IllegalArgumentException]

页面(400错误):

SpringMVC自定义日期转换器不起作用,报400错误,Failed to convert value of type xxx to required type xxx

错误原因+解决办法

错误代码(springmvc.xml)

	<!-- 把转换器工厂放入到注解驱动,转换器才会生效 -->
    <mvc:annotation-driven conversion-service="conversionServiceFactory" />

    <!--  开启注解驱动,开启SpringMVC的注解的支持 @RequestMapping @RequestBody @ResponseBody这些注解需要使用 -->
    <mvc:annotation-driven/>

错误原因
转换器工厂加入到注解驱动<mvc:annotation-driven conversion-service="conversionServiceFactory" />写在了开启注解驱动<mvc:annotation-driven/>的前面,注解驱动都没有开启,怎么将转换器工厂放入到注解驱动呢?

解决办法:
将开启注解驱动代码提前<mvc:annotation-driven/>,放在转换器工厂的前面,就可以解决问题了。

springmvc模板:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">

    <!--springmvc是web层(Controller处理请求)  UserController  @Controller -->
    <!--1.扫描Controller所在包-->
    <context:component-scan base-package="com.xgf.web"/>

    <!--2. 配置的视图解析器对象,success  路径/WEB-INF/pages/success.jsp -->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--3. 配置文件 - 过滤静态资源  前面的前端控制器拦截所有,静态css、js这些也都拦截
            配置使.js .css img这些静态文件不被拦截,保证静态文件都不拦截
             用过mvc标签进行放行,这些目录下文件都不拦截      -->
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/images/" mapping="/images/**" />
    <mvc:resources location="/js/" mapping="/js/**" />
    <mvc:resources location="/fonts/" mapping="/fonts/**" />

    <!-- 4. 开启注解驱动,开启SpringMVC的注解的支持 @RequestMapping @RequestBody @ResponseBody这些注解需要使用 -->
    <mvc:annotation-driven/>

    <!-- 5. 配置类型转换器  -->
    <!-- 创建类型转换器对象 -->
    <bean id="stringToDateConverter" class="com.xgf.web.converter.StringToDateConverter"/>
    <!-- 把转换器对象放入SpringMVC转换器工厂中 -->
    <bean id="conversionServiceFactory" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <!-- 引入日期转换器对象(上面创建的) -->
                <ref bean="stringToDateConverter"/>
                <!-- 或者直接将转换器类写到这里面 -->
                <!--<bean id="stringToDateConverter" class="com.xgf.web.converter.StringToDateConverter"/>-->
            </set>
        </property>
    </bean>

    <!--6. 把转换器工厂放入到注解驱动,转换器才会生效 -->
    <mvc:annotation-driven conversion-service="conversionServiceFactory" />
    
</beans>

本文地址:https://blog.csdn.net/qq_40542534/article/details/109249187