0

I am stuck in this part of code where I am getting HTTP 406 error.

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


I searched for two days, tried every possibilities and could not figure out the solution. The server I use is tomcat8.
This is my controller class:

@RestController
public class WebServiceController {


@RequestMapping(value="/testpage",method=RequestMethod.GET)
public TestObject test(){
    TestObject to = new TestObject();
    to.setAge("er");
    to.setName("sdfsdf");
    return to;
}
}

my TestObject:

public class TestObject {

private String name;
private String age;
public String getName() {
    return name;    }
public void setName(String name) {
    this.name = name;   }
public String getAge() {
    return age; }
public void setAge(String age) {
    this.age = age; }
}

my dispatcher servelet

<?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-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<mvc:annotation-driven/>

<context:component-scan base-package="com.kpos.webservice"/>

</beans> 

here is my pom.xml

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kpos.play.ws</groupId>
<artifactId>CFBService</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>CFBService Maven Webapp</name>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.5.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.5.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
</dependencies>
<build>
    <finalName>CFBService</finalName>
</build>

I read like if we use @RestController annotation, it will automatically convert object to JSON using jackson in classpath & @RestController automatically take care of @ResponseBody.

3
  • Could you copy/paste here the logs of your application, after configuring DEBUG level for org.springframework.web.servlet.mvc? I don't see anything wrong in your current configuration. Commented Mar 16, 2015 at 10:59
  • Note that you always should use version-less schemas in your dispatcher servlet configuration, i.e. replace http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd by http://www.springframework.org/schema/mvc/spring-mvc.xsd, especially since you're using version 4.1 here. Commented Mar 16, 2015 at 11:00
  • @BrianClozel No error logs.. All I can get is this below log line when the controller is initialized: INFO: Mapped "{[/testpage],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.kpos.webservice.objects.TestObject com.kpos.webservice.controller.WebServiceController.test() Commented Mar 20, 2015 at 17:05

1 Answer 1

1

Try to get all Jackson Libraries as the following Blog :

http://www.breathejava.com/restful-web-service-tutorial-spring-xml/

The problem is linked to a missing Library of Jackson.

Sign up to request clarification or add additional context in comments.

2 Comments

your example is using both Jackson 1.x and Jackson 2.x, which is wrong - Spring requires Jackson 2.1+ as of version 4.1. The OP has the right dependencies.
I got it working by adding '/' as servlet mapping in web.xml. I did added the above jars stil it was showing same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.