我们使用Spring MVC开发了一个标准的Java Web应用程序,并且最近尝试从3.0.6升级到3.2.0.几乎所有的servlet响应都是JSP或Json视图,但有一些是pdf请求,扩展名为"pdf".
在Spring 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>
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,与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'
Run Code Online (Sandbox Code Playgroud)
在调查了文档和一些博客之后,这个配置似乎有效:
<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> …Run Code Online (Sandbox Code Playgroud)