【问题标题】:Spring 3.2 content negotiation class cast exceptionSpring 3.2 内容协商类强制转换异常
【发布时间】:2013-01-31 17:49:27
【问题描述】:

我们使用 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? 谢谢

理查德

【问题讨论】:

  • 谢谢 - 但在删除“价值”标签后,我在运行 MVC 测试时仍然遇到同样的问题。

标签: spring spring-mvc content-negotiation


【解决方案1】:

我通过删除重复的问题解决了这个问题 &lt;mvc:annotation-driven/&gt; 来自 xml 配置 或者 来自类的@EnableWebMVC 注释 因为春季文档对此提出了警告,并且仅通过合同允许一次。

【讨论】:

  • 你是我的英雄。我会为你建造一座神社!谢谢。
  • 这个想法是在 xml-config 或 java-annotation-config 中选择配置格式。
  • 也许如果你有一个米奇老鼠项目,但在大公司的现实世界中它看起来不同;-)
  • quora.com/… 源于建设和发展。
  • @OleksiiKyslytsyn 即使只使用其中一个也会收到错误
【解决方案2】:

在 spring 3.2 中使用ContentNegotiationManager 解决问题会更好。它对我有用。您可以使用org.springframework.http.MediaType.APPLICATION_JSON_VALUE 之类的静态字段来提及媒体类型。检查以下代码:

<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 http://www.springframework.org/schema/util/spring-util-3.0.xsd

【讨论】:

  • 谢谢!这就是诀窍 - 实际上静态字段值需要是 org.springframework.http.MediaType.APPLICATION_JSON 和 org.springframework.http.MediaType.APPLICATION_XML 才能设置对象,但它工作正常。
【解决方案3】:

我认为使用 ContentNegotiationManagerFactoryBean 可以更轻松地实现相同的目标。默认情况下,它首先检查 URL 路径扩展名,然后是 URL 中的格式属性(例如 ..../accounts?format=pdf),然后是标准 HTTP Accept 标头属性。格式参数的使用默认是关闭的。

<bean id="contentNegotiationManager"
      class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="defaultContentType" value="text/html" />

    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
            <entry key="pdf" value="application/pdf" />
            <entry key="pdf" value="text/html" />
       </map>
    </property>
</bean>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="contentNegotiationManager" ref=""/>
</bean>

如果您使用 JavaBeans 激活框架 JAF,则不需要 mediaTypes 部分。只需将activation.jar 放在类路径中即可。

您是否尝试过此操作,是否还遇到了类强制转换异常?

【讨论】:

    【解决方案4】:

    好吧,您发布的错误基本上是说 Spring 没有办法将您提供的字符串(例如“application/pdf”)转换为 MediaType 对象。我猜 ContentNegotiatingViewResolver 将它的 mediaTypes 映射更改为 Map 到 Map。你可以试试这样的:

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
    <map>
      <entry key="pdf">
        <value>
          <bean class="org.springframework.http.MediaType">
            <constructor-arg value="application/pdf" />
          </bean>
        </value>
      </entry>
      <entry key="html">
        <value>
          <bean class="org.springframework.http.MediaType">
            <constructor-arg value="text/html" />
          </bean>
        </value>
      </entry>
      <entry key="json">
        <value>
          <bean class="org.springframework.http.MediaType">
            <constructor-arg value="application/json" />
          </bean>
        </value>
      </entry>
    </map>
    </bean>
    

    注意:我是凭记忆做的,所以我可能打错了。基本上,您需要的条目值是 MediaTypes,而不是字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-05
      • 2016-02-16
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      相关资源
      最近更新 更多