基于我之前的question,我已将Spring框架从3.0.2升级到3.2.0,原因在于此question(Spring 3.0.2导致上传多个问题)文件,因为有一个长期存在的错误)。
使用较新的版本,一切正常JSON。在回复时,Google Chrome会显示此错误。
无法加载资源:服务器响应状态为406 (不可接受)
我尝试按照dispatcher-servlet.xml
文件中的指定here进行操作,例如,
<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>
如何解决此异常? Google搜索results此异常无法向我提供具体的参考资料。
修改
根据this配置,我现在有以下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(其下载page)库。使用Jackson 2.1.1时,它无法在运行时抛出以下异常。
org.springframework.beans.factory.BeanCreationException:错误 用名字创建bean 'org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0' 在ServletContext资源[/WEB-INF/dispatcher-servlet.xml]中定义: 无法创建内部bean 'org.springframework.web.servlet.view.json.MappingJacksonJsonView#bb314f' 类型 [org.springframework.web.servlet.view.json.MappingJacksonJsonView] 用key [0]设置bean属性'defaultViews';嵌套 异常是org.springframework.beans.factory.BeanCreationException: 使用名称创建bean时出错 'org.springframework.web.servlet.view.json.MappingJacksonJsonView#bb314f' 在ServletContext资源[/WEB-INF/dispatcher-servlet.xml]中定义: bean的实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:不能 实例化bean类 [org.springframework.web.servlet.view.json.MappingJacksonJsonView]: 构造函数抛出异常; 嵌套异常是 java.lang.NoClassDefFoundError:org / codehaus / jackson / map / ObjectMapper
因为Jackson 2.1.1在另一个包中有ObjectMapper
类 - com.fasterxml.jackson.databind.ObjectMapper
。是什么方式?
答案 0 :(得分: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 schema
<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">
答案 1 :(得分: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
答案 2 :(得分: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并修复与上述配置相关的任何语法问题留给您。