2014-11-09 26 views
3

我使用spring 4.1.1.RELEASE幷包含:jackson-core-asl 1.9.13和jackson-mapper-asl 1.9.13 pom使用RestController創建一個簡單的應用程序。Spring 4 RestController JSON:根據請求「accept」頭文件不可接受的特徵

這裏是回購: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類有公共getter和setter。

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

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

當我通過瀏覽器,我得到發送請求,

該請求所標識的資源只能生成具有根據用戶的要求 「接受」頭不能接受的特點 響應。

我曾嘗試發送用頭其他REST工具請求作爲

接受:應用/ JSON

,甚至試圖$ .getJSON(),$。阿賈克斯()等。 ,但同樣的錯誤仍然出現。我已經嘗試了StackOverflow中的所有其他相關線程,但問題仍然存在。

+0

沒有什麼你的代碼錯誤,必須進行相關的依賴關係。你是否在使用maven,如果是的話,還需要粘貼pom – 2014-11-09 06:37:09

+0

好的,請和我一起粘貼並粘貼你的Sample類,我非常確信你的配置沒問題。 – 2014-11-09 07:32:32

+0

嗨,我推動代碼回購:https://github.com/robikshrestha/samplespringrest.git – coolscitist 2014-11-09 08:08:00

回答

11

這個錯誤的訣竅是它可能非常錯過。在與OP相同的情況下,您可以看到由瀏覽器GET請求(accept header */*)產生的錯誤以及正確的配置(在OP中爲默認的最小工作配置),原因很可能是轉換爲表示。

這裏即使請求沒有暗示表示(也參數,也不路徑,也不接受報頭),但是該響應被抱怨由該請求所標識的

資源僅能夠產生 的與特徵響應根據所述請求 「接受」報頭不能接受

的原因可能是:

  • 缺失的依賴關係
  • 返回bean中的錯誤 (例如,缺少吸氣等)

Spring框架4.1,最低傑克遜版本應該是2.1 (2.3 recommended),更換您的傑克遜這個單一的一個

<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> 
+0

非常感謝。這解決了它。我用Tomcat 8.0測試了它。 :-) – coolscitist 2014-11-09 11:31:45

+1

這是在Spring遷移指南中提到的。 https://github.com/spring-projects/spring-framework/wiki/Migrating-from-earlier-versions-of-the-Spring-Framework#libraries – 2014-11-11 11:34:54

+0

非常感謝! – 2015-02-11 20:34:36

相關問題