您的位置:首页 > 大数据 > 人工智能

Failed to load resource: the server responded with a status of 406 (Not Acceptable)问题的解决方案

2017-01-18 22:58 811 查看
使用SpringMVC时,我们经常需要在Controller里返回一个对象,以json的方式传给视图。

可能会遇到如下问题:

Failed to load resource: the server responded with a status of 406 (Not Acceptable)

引发这问题的可能性有好几种,我遇到这个是因为json转换的问题。

我们在SpringMVC的配置文件中加上如下配置即可:

<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
<bean
class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg index="0" value="UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>


当然要引入相关的jar包,我这里使用的是maven,需要的依赖如下:

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>


对应的jar包为:

jackson-mapper-asl-1.9.13.jar

jackson-core-asl-1.9.13.jar

jackson-annotations-2.6.3.jar

jackson-core-2.6.3.jar

jackson-databind-2.6.3.jar

如果用Maven下载包的时候太慢,可以直接使用我上传的资源:http://download.csdn.net/detail/lianjiww/9740986

把这个资源下载下来直接考到本地Maven仓库即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐