Spring 3.2内容协商类强制转换异常 [英] Spring 3.2 content negotiation class cast exception

查看:108
本文介绍了Spring 3.2内容协商类强制转换异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用Spring MVC开发了标准的Java Web应用程序,并且最近尝试将其从3.0.6升级到3.2.0.几乎我们所有的servlet响应都是JSP或Json视图,但是有些是pdf请求,扩展名为"pdf".

We develop a standard Java web application using Spring MVC, and have recently tried to upgrade from 3.0.6 to 3.2.0. Nearly of all our servlet responses are JSP or Json views, but there are some that are pdf requests, with extension 'pdf'.

春季3.0.6 我们已经从Spring MVC文档中获得了此设置.

In Spring 3.0.6 we had this set up, 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>

与XMLViewResolver结合使用时效果很好.

which worked fine, in combination with an XMLViewResolver.

更新到3.2.0后,出现故障:

After updating to 3.2.0, there is a failure :

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 investigating the docs and some blogs, this configuration seems to work:

<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异常,因此看来测试框架的初始化方式与应用程序运行时的初始化方式不同... 有谁对如何以健壮的方式在Spring 3,2中配置ContentNegotiatingViewResolver有清晰的解释? 谢谢

But, we have tried running the new Spring MVC test framework using this configuration, and get the ClassCast exception again, so it seems the test framework is not initialising the beans in the same way as when the application runs... Does anyone have a clear explanation of how to configure the ContentNegotiatingViewResolver in Spring 3,2 in a robust fashion? Thanks

理查德

推荐答案

对于spring 3.2,最好使用ContentNegotiationManager进行解析.它为我工作.您可以使用org.springframework.http.MediaType.APPLICATION_JSON_VALUE之类的静态字段来提及媒体类型.检查以下代码:

With spring 3.2 it is better resolved using ContentNegotiationManager. It is working for me. You could use the static field like org.springframework.http.MediaType.APPLICATION_JSON_VALUE to mention the mediatype. check the following code:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="contentNegotiationManager">
            <bean class="org.springframework.web.accept.ContentNegotiationManager">
                <constructor-arg>
                    <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
                        <constructor-arg>
                            <map>
                                <entry key="json">
                                    <util:constant static-field="org.springframework.http.MediaType.APPLICATION_JSON_VALUE" />
                                </entry>
                                <entry key="xml">
                                    <util:constant static-field="org.springframework.http.MediaType.APPLICATION_XML_VALUE" />
                                </entry>
                            </map>
                        </constructor-arg>
                    </bean>
                </constructor-arg>
            </bean>
        </property>

        <property name="defaultViews">
            <list>
               <!-- default views -->
            </list>
        </property>

    </bean>

为此,您必须在您的dispatcher-servlet.xml文件中使用util模式,即xmlns:util="http://www.springframework.org/schema/util"和模式位置 http://www.springframework.org/schema/util/spring-util -3.0.xsd

For this you have to use util schema in your dispatcher-servlet.xml file, i.e. xmlns:util="http://www.springframework.org/schema/util" and schema location http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd

这篇关于Spring 3.2内容协商类强制转换异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆