根据我之前的问题,由于这个问题中提到的原因,我已经将 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
. 方法是什么?