SpringMVC中AJAX请求出现406

原文:http://my.oschina.net/u/1756147/blog/366510


这几天因为需要搭建SpringMVC,但是搭建完成以后发现使用@ResponseBody的ajax无法访问,总是出现406的问题。

首先怀疑的是配置问题,经过查明,影响SpringMVC的@ResponseBody注解的是:

<mvc:annotation-driven />,我发现我的配置中存在这个注解。同时又使用Spring文档中的自动配置相关解析类的方式再进行测试,发现还是没有解决问题。

同时在网上找到相关问题,发现是缺失jackson的jar。

   <dependency> 
            <groupId>org.codehaus.jackson</groupId> 
            <artifactId>jackson-core-lgpl</artifactId> 
            <version>1.9.0</version> 
        </dependency> 
        <dependency> 
            <groupId>org.codehaus.jackson</groupId> 
            <artifactId>jackson-mapper-lgpl</artifactId> 
            <version>1.9.0</version> 
    </dependency>

于是加上该依赖,可是发现问题还是没有解决。

然后我就下载当前我使用的Spring版本的官方文档来看。

意外发现如下:

 Spring 4 requires the recent Hibernate Validator 4.3+, and support for Jackson has been focused on 2.0+ now (with Jackson 1.8/1.9 support retained for the time being where Spring 3.2 had it; now just in deprecated form).

因为我用的是4.1.4的版本,所以已经不支持1.9以下的jackson了。

为了测试,我将我spring的版本改回3.2+的版本。发现ajax正常通过。因此又将版本改回,然后把jaskson的版本改为2.5

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-core</artifactId>

<version>2.5.0</version>

</dependency>

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.5.0</version>

</dependency>

其实只需要下面的databind即可,因为该配置会自动加上core的配置。不过为了便于理解我将这两个都加上。

果然修改以后即可成功通过。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值