3

根据我之前的问题,由于这个问题中提到的原因,我已经将 Spring 框架从 3.0.2 升级到了3.2.0(Spring 3.0.2 导致上传多个文件时出现问题,因为存在长期存在的错误)。

使用较新的版本,一切正常,但 JSON 除外。响应时,谷歌浏览器显示此错误。

加载资源失败:服务器响应状态为 406(不可接受)

我试图按照文件中的说明dispatcher-servlet.xml进行操作,例如,

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <property name="mediaTypes">
    <map>
      <entry key="atom" value="application/atom+xml"/>
      <entry key="html" value="text/html"/>
      <entry key="json" value="application/json"/>
    </map>
  </property>
  <property name="viewResolvers">
    <list>
      <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
      </bean>
    </list>
  </property>
  <property name="defaultViews">
    <list>
      <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
    </list>
  </property>
</bean>

并在保存 xml 文件时以以下异常结束。

java.lang.ClassCastException java.lang.String 无法转换为 org.springframework.http.MediaType


文件完整内容dispatcher-servelet.xml如下,有需要的可以看看。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"

       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <context:component-scan base-package="controller" />
    <context:component-scan base-package="validatorbeans" />
    <mvc:annotation-driven />




<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <property name="mediaTypes">
    <map>
      <entry key="atom" value="application/atom+xml"/>
      <entry key="html" value="text/html"/>
      <entry key="json" value="application/json"/>
    </map>
  </property>
  <property name="viewResolvers">
    <list>
      <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
      </bean>
    </list>
  </property>
  <property name="defaultViews">
    <list>
      <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
    </list>
  </property>
</bean>

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>

            </props>
        </property>
    </bean>

<!--<bean id="viewResolver"
       class="org.springframework.web.servlet.view.InternalResourceViewResolver"
       p:prefix="/WEB-INF/jsp/"
       p:suffix=".jsp" />

       Initially this bean was mentioned like this was given a comment. It is specified as above-->

    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />
</beans>

如何解决此异常?这个例外的谷歌搜索结果无法给我带来具体的参考。


编辑:

根据这个配置,我现在有以下 XML 映射。

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1" />
    <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" value="application/json"/>
                            <entry key="xml" value="application/xml"/>
                            <!--<entry key="html" value="text/html"/>
                            <entry key="atom" value="application/atom+xml"/-->
                        </map>
                    </constructor-arg>
                </bean>
            </constructor-arg>
        </bean>
    </property>


    <property name="defaultViews">
        <list>
            <!-- JSON View -->
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />

            <!-- XML View -->
            <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                <constructor-arg>
                    <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
                        <property name="packagesToScan">
                            <list>
                                <value>documentLoader.domain</value>
                            </list>
                        </property>
                    </bean>
                </constructor-arg>
            </bean>
        </list>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

消失了ClassCastException,但 JSON 仍然不起作用。我仍然遇到同样的错误,

加载资源失败:服务器响应状态为 406(不可接受)

还剩下什么?仍然可以看到一个jira问题。

注意:我在类路径上有 Jackson 1.9.8(它的下载页面)库。对于 Jackson 2.1.1,在运行时抛出以下异常不起作用。

org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/dispatcher-servlet.xml] 中定义名称为“org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0”的 bean 创建错误:无法创建内部bean 'org.springframework.web.servlet.view.json.MappingJacksonJsonView#bb314f' 类型 [org.springframework.web.servlet.view.json.MappingJacksonJsonView],同时使用键 [0] 设置 bean 属性'defaultViews';嵌套异常是 org.springframework.beans.factory.BeanCreationException: Error Creating bean with name 'org.springframework.web.servlet.view.json.MappingJacksonJsonView#bb314f' 在 ServletContext 资源 [/WEB-INF/dispatcher-servlet.xml ]: bean 实例化失败;嵌套异常是 org.springframework.beans。嵌套异常是 java.lang.NoClassDefFoundError: org/codehaus/jackson/map/ObjectMapper

因为 Jackson 2.1.1ObjectMapper在另一个包中有这个类 - com.fasterxml.jackson.databind.ObjectMapper. 方法是什么?

4

3 回答 3

4

最好在 MediaType 类中引用常量

<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_ATOM_XML_VALUE" />
        </entry>
    </map>
</constructor-arg>

您还需要包含 util 架构

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
于 2013-01-14T21:14:48.767 回答
2

您应该添加对 ContentNegotiatingViewResolver 的引用。代替

<mvc:annotation-driven />

经过

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />

并将 id 添加到 bean 定义中:

<bean id="contentNegotiationManager"  class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">

此外,您需要将架构位置更新为 3.2。

xsi:schemaLocation="
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
于 2013-10-01T11:34:35.680 回答
1

当您尝试将mediaTypes属性设置为时会发生异常,ContentNegotiatingViewResolver因为该值不是您想象的“text/html”之类的纯字符串。这是另一种类型MediaType

而是指定这样的内容。

<bean id="atomXml" class="org.springframework.http.MediaType">
<constructor-arg><value><![CDATA[application/atom+xml]]></value></constructor-arg>
</bean>

和其他豆类类似。


当你想设置 mediaTypes 时,将上面创建的 bean 注入到ContentNegotiatingViewResolver.

<property name="mediaTypes">
    <map>
      <entry key="atom" value-ref="atomXml"/>
    </map>
</property>

定义其他相关 bean 并修复与上述配置相关的任何语法问题留给您作为练习。

于 2012-12-30T03:50:47.090 回答