406错误解决办法 SpringMVC

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>

 

猜你喜欢

转载自blog.csdn.net/zxgmdzz/article/details/80799876