3

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.

Here is the repo: https://github.com/robikshrestha/samplespringrest.git

Here is the failing 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>

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>

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>

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.

I have tried sending request with other REST tools using header as

Accept:application/json

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.

coolscitist
  • 3,317
  • 8
  • 42
  • 59
  • there's nothing wrong with your code, must be related to dependencies. Are you using maven, if so do paste pom as well – Master Slave Nov 09 '14 at 06:37
  • OK, please bare with me and paste your Sample class, I'm pretty confident that your configuration is OK. – Master Slave Nov 09 '14 at 07:32
  • Hi, I pushed the code to repo: https://github.com/robikshrestha/samplespringrest.git – coolscitist Nov 09 '14 at 08:08
  • Sample class: https://github.com/robikshrestha/samplespringrest/blob/master/src/main/java/com/sample/Sample.java – coolscitist Nov 09 '14 at 08:08
  • well, I hope you don't mind I added an answer as a general comment, for whoever is facing the issue. Now that I have the code, in addition to all looking good, I can tell you that all is running ok, /getSample ({"id":1,"name":"abc"}) and /list ([{"id":1,"name":"abc"}])return JSON results for me. Maybe you're facing some caching issues? – Master Slave Nov 09 '14 at 08:19
  • Why does cache affect anything? I am going to try by clearing the cache. – coolscitist Nov 09 '14 at 08:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64570/discussion-between-master-slave-and-coolscitist). – Master Slave Nov 09 '14 at 08:24

1 Answers1

11

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

The causes could be:

  • The missing dependencies
  • Error in the return bean (e.g. missing getters or the like)

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>

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 version should still use the

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
Master Slave
  • 27,771
  • 4
  • 57
  • 55