SpringMVC custom date converter does not work, report 400 error, Failed to convert value of type xxx to required type xxx

wrong description

Console:

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]

Page (400 error):

Insert picture description here

Error reason + solution

Error code (springmvc.xml)

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

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

The cause of the error The
converter factory is added to the annotation driver and <mvc:annotation-driven conversion-service="conversionServiceFactory" />written before the <mvc:annotation-driven/>annotation driver is turned on. The annotation driver is not turned on. How to put the converter factory into the annotation driver?

Solution:
Put the opening annotation driver code in advance <mvc:annotation-driven/>and put it in front of the converter factory to solve the problem.

springmvc template:

<?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>

Guess you like

Origin blog.csdn.net/qq_40542534/article/details/109249187