We are developing a standard Java web application using Spring MVC and recently tried to upgrade from 3.0.6 to 3.2.0. Almost all responses to the servlet are JSP or Json representations, but there are some that are pdf requests with the extension βpdfβ.
In Spring 3.0.6, we created this setting, taken from the Spring MVC documentation.
<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>
which worked great in conjunction with XMLViewResolver.
After upgrading to 3.2.0, a crash occurred:
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'
After examining the docs and some blogs, this configuration works:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="contentNegotiationManager"> <bean class="org.springframework.web.accept.ContentNegotiationManager"> <constructor-arg> <list> <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> <bean class="org.springframework.web.accept.HeaderContentNegotiationStrategy"> </bean> </list> </constructor-arg> </bean> </property>
But we tried to launch the new MVC Spring test infrastructure using this configuration and get a ClassCast exception again, so it seems that the test environment does not initialize beans the same way as when the application starts ... Does anyone have a clear explanation on how to configure the ContentNegotiationViewResolver in Spring 3.2 in a reliable way? thanks
Richard
source share