SpringMVC出现406错误的解决办法

来源:互联网 发布:阿里云国际版注册攻略 编辑:程序博客网 时间:2024/05/17 23:14

1.因为spring 3.x(具体哪个版本忘记了)开始,对request里的header 中的accept(也就是mimetype)进行了识别,如果你指定了拦截后缀,比如你原先的.html,那么不管你是ajax还是平常的页面访问,都会当作text/html处理,而你通过ajax访问并不是text/html,而是application/json(可能是类似的),于是spring mvc给你报了个406错误。

2.为此你需要引入Jackson-Annotations-2.4.6.jar,Jackson-core-2.4.6.jar,Jackson-databind-2.4.6.jar三个jar包,这样在使用@ResponseBody注解时才会自动帮你打包成json格式。

3.maven pom.xml引入地址:

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->        <dependency>            <groupId>com.fasterxml.jackson.core</groupId>            <artifactId>jackson-core</artifactId>            <version>2.4.6</version>        </dependency>        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->        <dependency>            <groupId>com.fasterxml.jackson.core</groupId>            <artifactId>jackson-databind</artifactId>            <version>2.4.6</version>        </dependency>        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->        <dependency>            <groupId>com.fasterxml.jackson.core</groupId>            <artifactId>jackson-annotations</artifactId>            <version>2.4.6</version>        </dependency>
1 0