有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Spring MVC“未找到能够从java.lang.String类型转换为org.springframework.core.io.Resource类型的转换器”

我们有许多自定义的“org.springframework.core.convert.converter.converter”转换器,可以帮助我们自动将rest请求中的URL参数映射到域对象

今天,我们通过定义转换服务注册了这些转换器:

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <!-- List of custom converter beans here -->
        </set>
    </property>
</bean>

然后通过以下方式进行注册:

<mvc:annotation-driven conversion-service="conversionService" />

然而,对于我们的每一个定义,我们在启动时都会遇到以下例外情况:

org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.util.ArrayList<?> to type java.util.List<org.springframework.core.io.Resource> for value '[/WEB-INF/images/]'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.lang.String to type org.springframework.core.io.Resource

以前有人遇到过/解决过这个问题吗

我们使用的是spring 3.2.9


共 (2) 个答案

  1. # 1 楼答案

    我很高兴你把它整理好了。我拉了这个项目,自己运行了它,只是为了看一看,你的解释是正确的。你可以把这个添加到你的log4j中。xml,如果您希望在将来避免该消息污染日志:-)

    <logger name="org.springframework.beans.TypeConverterDelegate">
        <level value="error" />
    </logger>
    
  2. # 2 楼答案

    我最近也遇到了这个问题

    我的Spring版本是4.1。x、 一开始,我只使用FormattingConversionServiceFactoryBean来配置我定制的转换器,如下所示:

    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="registerDefaultFormatters" value="false" />
        <property name="converters">
            <set>
                <bean class="com.example.controller.converter.CustomerStringToDateConverter"/>
            </set>
        </property>
    </bean>
    
    <mvc:annotation-driven conversion-service="conversionService"/>
    

    效果很好

    但当我使用“资源服务”时,它抛出了异常

    <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>
    
    DEBUG [RMI TCP Connection(3)-127.0.0.1] - Original ConversionService attempt failed - ignored since PropertyEditor based conversion eventually succeeded
    org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.util.ArrayList<?> to type java.util.List<org.springframework.core.io.Resource> for value '[/js/]'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.util.ArrayList<?> to type org.springframework.core.io.Resource
    

    解决这个问题的方法是更改“FormattingConversionServiceFactoryBean”的id名称,例如“conversionService2”

    <mvc:annotation-driven conversion-service="conversionService"/>
    

    同时更改此引用