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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24986367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:12:16  来源:igfitidea点击:

Spring MVC "No converter found capable of converting from type java.lang.String to type org.springframework.core.io.Resource"

javaspringspring-mvctype-conversion

提问by Jamie Cramb

We have a number of custom "org.springframework.core.convert.converter.Converter" converters that help us automatically map URL params in rest requests to our domain objects.

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

Today we have these converters registered by defining a conversion service:

今天我们通过定义一个转换服务来注册这些转换器:

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

and then registering it using:

然后使用以下方法注册它:

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

However we get the following exception on startup for each of our definitions:

但是,对于我们的每个定义,我们在启动时都会遇到以下异常:

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

Has anyone encountered / resolved this before?

有没有人遇到/解决过这个问题?

We are using spring 3.2.9.

我们使用的是 spring 3.2.9。

采纳答案by dectarin

Im glad you got it sorted anyway. I pulled the project and ran it myself anyway just to take a look and your explanation is correct. You could add this to your log4j.xml if you wanted to get rid of that message from polluting your logs in the future :-)

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

<logger name="org.springframework.beans.TypeConverterDelegate">
    <level value="error" />
</logger>

回答by Jamie Cramb

Turns out this was just me not reading the line above the stack trace properly... missed the fact that the line above the stack was "DEBUG".

原来这只是我没有正确阅读堆栈跟踪上方的行......错过了堆栈上方的行是“调试”这一事实。

In short, the conversion service is unable to convert from a String to a Resource; however, spring falls back on it's PropertyEditor-based conversion which succeeds (I believe this is how String-Resource conversions are done normally).

简而言之,转换服务无法从 String 转换为 Resource;然而,spring 依赖于它成功的基于 PropertyEditor 的转换(我相信这是字符串资源转换的正常完成方式)。

I created a sample project that will re-create this debug exception: https://github.com/jamiecramb/pastebin/tree/master/conversion-test.

我创建了一个示例项目,它将重新创建此调试异常:https: //github.com/jamiecramb/pastebin/tree/master/conversion-test

Example of the full stack was as follows:

完整堆栈的示例如下:

DEBUG: org.springframework.beans.TypeConverterDelegate - 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 '[/WEB-INF/css/]'; 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
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:168)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:161)
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:448)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:494)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:488)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1463)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1422)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1158)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:633)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:602)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:521)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:462)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5198)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5481)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:634)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:671)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1840)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.lang.String to type org.springframework.core.io.Resource
    at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:276)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:171)
    at org.springframework.core.convert.support.CollectionToCollectionConverter.convert(CollectionToCollectionConverter.java:83)
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:35)
    ... 40 more

回答by Ludovic Guillaume

I just had the error with Spring 4.1.1 and the same definition of FormattingConversionServiceFactoryBean.

我只是在 Spring 4.1.1 和FormattingConversionServiceFactoryBean.

Hiding logs won't resolve the issue... and in my opinion, it's not a viable solution, especially for production.

隐藏日志并不能解决问题……在我看来,这不是一个可行的解决方案,尤其是对于生产而言。

I fixed it by changing <set></set>to <list></list>.

我通过更改<set></set><list></list>.

回答by Sui

I also met this question recently.

我最近也遇到了这个问题。

My Spring version is 4.1.x.In the beginning,I only use the FormattingConversionServiceFactoryBeanto config my customed converter like this:

我的 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"/>

It works well.

它运作良好。

But when I use the "Serving of Resources",it throws exception

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

<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

The way to solve this question is change the id name of the "FormattingConversionServiceFactoryBean" ,for example "conversionService2".

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

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

and change this refer at the same time

并同时更改此引用