mvc:resources导致ClassCastException

时间:2012-01-10 20:16:46

标签: java spring-mvc

我有一个应该提供静态资源的Spring MVC webapp。 因此我有以下弹簧配置:

<mvc:resources location="/resources/" mapping="/resources/**"/>

但是对资源的每个请求都会导致同样丑陋的ClassCastException:

java.lang.ClassCastException: java.lang.String cannot be cast to org.springframework.core.io.Resource
at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.getResource(ResourceHttpRequestHandler.java:158)
at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.handleRequest(ResourceHttpRequestHandler.java:109)
at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.myproject.filter.SetP3PHeaderFilter.doFilter(SetP3PHeaderFilter.java:48)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.myproject.filter.SessionIdUrlFilter.disableDefaultURLEncoding(SessionIdUrlFilter.java:83)
at com.myproject.filter.SessionIdUrlFilter.avoidSessionFixation(SessionIdUrlFilter.java:53)
at com.myproject.filter.SessionIdUrlFilter.doFilter(SessionIdUrlFilter.java:42)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)

我尝试使用版本3.1.0.RELEASE和Spring框架的3.0.4.RELEASE并观察到完全相同的行为。

2 个答案:

答案 0 :(得分:1)

您的配置中可能缺少mvc:annotation-driven,即注册属性编辑器以从字符串转换为资源的那个,这可能会导致此错误消息。

答案 1 :(得分:1)

这真的很奇怪,但问题似乎是,我已经定义了这样的转换服务:

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="converters">
        <list>
           <bean    class="com.example.CustomConverter" />
        </list>
    </property>
</bean>

这里引用了:

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

如果我将bean id更改为与f不同的东西,那么结果如何。即cs它运作正常。这很奇怪,因为在文档和展示中使用了相同的ID。