spring – 无法加载资源:服务器响应状态为406(不可接受)
作者:互联网
基于我之前的question,我已经将Spring框架从3.0.2升级到3.2.0,因为question中提到的原因(Spring 3.0.2导致上传多个文件时出现问题,因为存在长期存在的错误).
对于较新的版本,一切正常JSON.在回复时,Google Chrome会显示此错误.
Failed to load resource: the server responded with a status of 406
(Not Acceptable)
我试图在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 cannot be cast to
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>
如何解决此异常?谷歌搜索这个例外的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仍然不起作用.我仍然得到同样的错误,
Failed to load resource: the server responded with a status of 406
(Not Acceptable)
还剩下什么?还有一个jira问题还可以看出来.
注意:我在类路径上有Jackson 1.9.8(其下载page)库.使用Jackson 2.1.1,它在运行时抛出以下异常无效.
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
‘org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0’
defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]:
Cannot create inner bean
‘org.springframework.web.servlet.view.json.MappingJacksonJsonView#bb314f’
of type
[org.springframework.web.servlet.view.json.MappingJacksonJsonView]
while setting bean property ‘defaultViews’ with key [0]; nested
exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name
‘org.springframework.web.servlet.view.json.MappingJacksonJsonView#bb314f’
defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]:
Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Could not
instantiate bean class
[org.springframework.web.servlet.view.json.MappingJacksonJsonView]:
Constructor threw exception; nested exception is
java.lang.NoClassDefFoundError: org/codehaus/jackson/map/ObjectMapper
因为Jackson 2.1.1在另一个包中有类ObjectMapper – com.fasterxml.jackson.databind.ObjectMapper.这是什么方式?
解决方法:
当您尝试将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并修复与上述配置相关的任何语法问题仍然可以作为练习.
标签:spring,dependency-injection,spring-3 来源: https://codeday.me/bug/20190729/1569727.html