Spring 4 RestController JSON:根据请求“接受"不可接受的特性标题 [英] Spring 4 RestController JSON: characteristics not acceptable according to the request "accept" headers

查看:56
本文介绍了Spring 4 RestController JSON:根据请求“接受"不可接受的特性标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring 4.1.1.RELEASE 并在 pom 中包含:jackson-core-asl 1.9.13 和 jackson-mapper-asl 1.9.13 来创建一个带有 RestController 的简单应用程序.

I am using spring 4.1.1.RELEASE and have included: jackson-core-asl 1.9.13 and jackson-mapper-asl 1.9.13 in pom to create a simple app with a RestController.

这里是存储库:https://github.com/robikshrestha/samplespringrest.git

这是失败的战争:https://github.com/robikshrestha/samplespringrest/树/主/失败战争

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mycompany</groupId>
    <artifactId>SampleContactApp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>SampleContactApp</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.1.1.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

这是我的 web.xml:

Here is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    <servlet>
        <servlet-name>sample</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>sample</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

和我的 sample-servlet.xml

and my sample-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.sample" />
    <mvc:annotation-driven />
</beans>

控制器类也很简单.而且 Sample 类确实有公共的 getter 和 setter.

The controller class is simple too. And Sample class does have public getters and setters.

@RestController
@RequestMapping("/")
public class SampleController {

    @RequestMapping("/getSample")
    public Sample getSample() {
        Sample s = new Sample();
        s.setId(1);
        s.setName("abc");
        return s;
    }
}

当我通过浏览器发送请求时,

When I send request through browser I get,

这个请求标识的资源只能生成根据请求具有不可接受特征的响应接受"标题.

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.

我尝试使用标头作为

接受:应用程序/json

Accept:application/json

甚至尝试了 $.getJSON()、$.ajax() 等.,但仍然出现同样的错误.我已经在 StackOverflow 中尝试了所有其他相关线程,但问题仍然存在.

and even tried $.getJSON(), $.ajax() etc. , but same error still comes up. I have tried all other related threads in StackOverflow, but problem still persists.

推荐答案

这个错误的诀窍在于它可能会非常容易误导.在与 OP 一样的情况下,您会看到浏览器 GET 请求(带有 accept header */*)和正确配置(在 OP 情况下为默认的最小工作配置)导致的错误,原因很可能是转换为表示时的异常.

The trick with this error is that it can be very miss-leading. In the situation as with the OP, where you see the error resulting from a browser GET request (with accept header */*), and the proper configuration (in the OPs case a default minimal working configuration) , the cause is very likely the exception while converting to representation.

这里即使请求没有建议表示(也不是参数,也不是路径,也不是接受标头),但响应却在抱怨

Here even though the request is not suggesting the representation (Nor parameter, nor path, nor accept header), yet the response is complaining about the

此请求标识的资源只能生成根据请求具有不可接受特征的响应接受"标题

resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers

原因可能是:

  • 缺少的依赖
  • 返回 bean 中的错误(例如缺少吸气剂等)

Spring Framework 4.1 起,最低 jackson 版本应为 2.1 (推荐 2.3),用这个替换你的 jackson 依赖

as of Spring Framework 4.1, the minimum jackson version should be 2.1 (2.3 recommended), replace your jackson dependencies with this single one

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.1.2</version>
    </dependency>

在这种情况下阻碍调试的一件事是,在 tomcat 7.0.5x 版本中,此依赖项在 libs 中可用,与某些以前的版本不同.所以你的代码在那个版本的 tomcat 中工作正常

One thing that hinders debugging in this case is that in tomcat 7.0.5x versions, this dependency is available in libs, unlike some previous version. So your code works fine in that version of tomcat just as it is

Spring MVC 3.x 版本应该仍然使用

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

这篇关于Spring 4 RestController JSON:根据请求“接受"不可接受的特性标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆