原文: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的配置。不过为了便于理解我将这两个都加上。
果然修改以后即可成功通过。