gpt4 book ai didi

rest - Spring 4 RestController JSON : characteristics not acceptable according to the request "accept" headers

转载 作者:行者123 更新时间:2023-12-01 06:10:17 28 4
gpt4 key购买 nike

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

这是 repo :https://github.com/robikshrestha/samplespringrest.git

这是失败的 war :https://github.com/robikshrestha/samplespringrest/tree/master/failingWar

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:
<?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
<?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>

Controller 类也很简单。 Sample 类确实有公共(public)的 getter 和 setter。
@RestController
@RequestMapping("/")
public class SampleController {

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

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

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



我尝试使用 header 作为其他 REST 工具发送请求

Accept:application/json



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

最佳答案

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

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

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



原因可能是:
  • 缺少的依赖项
  • 返回 bean 中的错误
    (例如缺少 setter/getter 等)


  • 截至 Spring 框架 4.1 , 最低 jackson 版本应该是 2.1 (2.3 recommended) ,用这个单一的替换你的 jackson 依赖
        <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.1.2</version>
    </dependency>

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

    Spring MVC 3.x 版本仍应使用
        <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
    </dependency>

    关于rest - Spring 4 RestController JSON : characteristics not acceptable according to the request "accept" headers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26825276/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com