我正在使用Spring 4.1.1.RELEASE,并在pom中包括:jackson-core-asl 1.9.13和jackson-mapper-asl 1.9.13,可使用RestController创建一个简单的应用程序。
这是仓库:https : //github.com/robikshrestha/samplespringrest.git
这是失败的战争: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>
控制器类也很简单。Sample类确实具有公共获取器和设置器。
@RestController @RequestMapping("/") public class SampleController { @RequestMapping("/getSample") public Sample getSample() { Sample s = new Sample(); s.setId(1); s.setName("abc"); return s; } }
通过浏览器发送请求时,
由该请求标识的资源仅能够生成具有根据请求“接受”标头不可接受的特征的响应。
我尝试使用标头作为其他REST工具发送请求
接受:application / json
甚至尝试了$ .getJSON(),$。ajax()等,但是仍然出现相同的错误。我已经尝试了StackOverflow中的所有其他相关线程,但是问题仍然存在。
出现此错误的诀窍是可能导致误导。在OP的情况下,您会看到浏览器GET请求(带有accept header */*)和正确的配置(在OP情况下为默认的最小工作配置)导致的错误,原因很可能是转换为表示形式时的异常。
accept header */*
即使请求没有建议表示形式(Nor参数,路径,接受标头),响应仍在抱怨
该请求标识的资源只能根据请求“接受”标头生成特性不可接受的响应
原因可能是:
从 Spring Framework 4.1开始 ,最低杰克逊版本应为2.1 (建议为2.3),用此单个杰克逊依赖项替换
<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>