Spring 3.2内容协商类强制转换异常

ott*_*606 4 spring spring-mvc content-negotiation

我们使用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>
<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>
Run Code Online (Sandbox Code Playgroud)

但是,我们尝试使用此配置运行新的Spring MVC测试框架,并再次获得ClassCast异常,因此测试框架似乎没有像应用程序运行时那样初始化bean ...有没有人有一个清楚地解释如何以健壮的方式在Spring 3,2中配置ContentNegotiatingViewResolver?谢谢

理查德

Ole*_*syn 14

我通过从类中删除<mvc:annotation-driven/>xml-configuration或@EnableWebMVCannotation中的重复来 解决问题, 因为spring文档会对此进行警告,并且只允许通过契约进行一次.

  • 你是我的英雄.我会为你建造一座神殿!谢谢. (3认同)

Sri*_*har 7

使用弹簧3.2,可以更好地解决使用问题ContentNegotiationManager.它对我有用.您可以使用静态字段org.springframework.http.MediaType.APPLICATION_JSON_VALUE来提及mediatype.检查以下代码:

<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>
Run Code Online (Sandbox Code Playgroud)

为此,您必须在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