1

I have written a simple rest based controller using @responseBody which I expect to return a JSON. Somehow I am not able to get it work as expected. when I run the url "http://localhost:8080/my-webapp/amazon/rips" ..it throws back below error

HTTP Status 406 -JBWEB000126: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request 'Accept' headers. can someone please lend a helping hand.

Mycontroller is below:

@Controller
@RequestMapping("/amazon")
public class JsonController {

@RequestMapping(value="/{name}", method = RequestMethod.GET,produces = "application/json")
public @ResponseBody
Book getShopInJSON(@PathVariable String name) {

    Book book = new Book();
    book.setName(name);
    book.setAvailablity(false);

    return book;

}

Book class is below:

public class Book {

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public boolean isAvailablity() {
    return availablity;
}

public void setAvailablity(boolean availablity) {
    this.availablity = availablity;
}

private String name ;
private boolean availablity;

}

displatcher servlet is as below:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="com.rips.controller" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<dependencies>
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
      <version>3.1.1.RELEASE</version>
  </dependency>
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>3.1.1.RELEASE</version>
  </dependency>

  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>3.1.1.RELEASE</version>
  </dependency>

  <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
  </dependency>

  <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>${jackson.version}</version>
  </dependency>
  <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-core-asl</artifactId>
      <version>${jackson.version}</version>
  </dependency>
3
  • Your request is comming in with an Accept header that doesn't include application/json. Thus, you get the 406. This is not a problem with the server-side code, but rather your client code. Commented Mar 24, 2014 at 11:44
  • the way I am testing is just calling localhost:8080/my-webapp/amazon/rips from my browser...is there anything wrong with this ? Commented Mar 24, 2014 at 11:46
  • 1
    Yes, it's sending the Accept as text/html. You need to call it in a way that sets the Accept header to 'application/json'. For instance you could use curl. Or write a dummy HTML page that does an AJAX call that sets it. Commented Mar 24, 2014 at 12:17

1 Answer 1

3

with the help from @CodeChimp I realized that request that I was sending was not having proper accept headers. I used Chromes "Advanced Rest client" and added headers with key ="accept" and value ="application/json",I was able to get proper response.

update I found that <mvc:annotation-driven /> was not added in the dispatcher servlet which configures the support for HTTP message conversion with @RequestBody/@ResponseBody.Once I added this piece of info there was no need to use any advanced Rest client.

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

Comments

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.