我们使用 Spring MVC 开发一个标准的 Java Web 应用程序,最近尝试从 3.0.6 升级到 3.2.0。我们几乎所有的 servlet 响应都是 JSP 或 Json 视图,但也有一些是 pdf 请求,扩展名为“pdf”。
春季3.0.6
我们进行了这样的设置,取自 Spring MVC 文档。
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="pdf" value="application/pdf"/>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
与 XMLViewResolver 结合使用效果很好。
更新到3.2.0后,出现故障:
Error creating bean with name' org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0' defined in class path resource [dispatcher-test-servlet.xml]: Invocation of init method failed; nested exception is
java.lang.ClassCastException: java.lang.String cannot be cast to org.springframework.http.MediaType'
在研究了文档和一些博客之后,此配置似乎有效:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager">
<bean class="org.springframework.web.accept.ContentNegotiationManager">
<constructor-arg>
<list>
<!-- These are evaluated in order -->
<!-- Is there a media type based on suffix? -->
<bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
<constructor-arg>
<map>
<entry key="html" value="text/html" />
<entry key="json" value="application/json" />
<entry key="pdf" value="application/pdf" />
</map>
</constructor-arg>
</bean>
<!-- Else use request header -->
<bean
class="org.springframework.web.accept.HeaderContentNegotiationStrategy">
</bean>
</list>
</constructor-arg>
</bean>
</property>
但是,我们尝试使用此配置运行新的 Spring MVC 测试框架,并再次收到 ClassCast 异常,因此测试框架似乎没有以与应用程序运行时相同的方式初始化 bean...
有谁对如何在 Spring 3,2 中以稳健的方式配置 ContentNegotiatingViewResolver 有清晰的解释吗?
谢谢
Richard
我通过删除重复的内容解决了这个问题<mvc:annotation-driven/>
来自 xml 配置
或者@EnableWebMVC
来自类的注释
因为 spring 文档警告了这一点,并且合同只允许一次。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)