title: Spring MVC + JSON = 406 Not Acceptable
body: <p>I'm trying to generate a simple JSON response working. Right now I get 406 Not Acceptable error. Tomcat says "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers." even though my <code>Accept</code> headers are</p>
  
  <pre><code>Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  </code></pre>
  
  <p>In tomcat/lib I have all Tomcat jars, Spring jars and jackson-all-1.9.0.jar. I'm using Spring 3.2.2 with Tomcat 7. </p>
  
  <p>I'm aware that this issue has been discussed many times, but none of solutions is working for me.</p>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;web-app id="WebApp_ID" version="2.4" 
      xmlns="http://java.sun.com/xml/ns/j2ee" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
  
    &lt;display-name&gt;Spring Web MVC Application&lt;/display-name&gt;
  
    &lt;servlet&gt;
      &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;
                    org.springframework.web.servlet.DispatcherServlet
          &lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
  
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.htm&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>dispatcher-servlet.xml</strong></p>
  
  <pre><code>&lt;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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"&gt;
  
      &lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver" &gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/pages/&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="suffix"&gt;
              &lt;value&gt;.jsp&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
   &lt;context:component-scan base-package="com.smiechmateusz.controller" /&gt;
   &lt;context:annotation-config /&gt;
  
      &lt;mvc:annotation-driven /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>HelloWorldController.java</strong></p>
  
  <pre><code>package com.smiechmateusz.controller;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.ResponseBody;
  import org.springframework.web.servlet.ModelAndView;
  import org.springframework.web.servlet.mvc.AbstractController;
  
  import com.smiechmateusz.dao.Foo;
  
  @Controller
  @RequestMapping("/")
  public class HelloWorldController extends AbstractController{
  
      @Override
      protected ModelAndView handleRequestInternal(HttpServletRequest request,
          HttpServletResponse response) throws Exception {
  
          ModelAndView model = new ModelAndView("HelloWorldPage");
          return model;
      }
  
      @RequestMapping(value="foobar.htm", method = RequestMethod.GET)
      public @ResponseBody Foo getShopInJSON() {
          Foo f = new Foo();
          f.setX(1);
          f.setY(2);
          f.setDescription("desc");
          return f;
      }
  }
  </code></pre>
  
  <p><strong>Foo.java</strong></p>
  
  <pre><code>package com.smiechmateusz.dao;
  
  import java.io.Serializable;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  @Entity
  @Table(name="foobaz")
  public class Foo implements Serializable
  {
      private int x, y;
      String description;
      int id;
  
      @Column(name = "x")
      public int getX() {
          return x;
      }
      public void setX(int x) {
          this.x = x;
      }
      @Column(name = "y")
      public int getY() {
          return y;
      }
      public void setY(int y) {
          this.y = y;
      }
      @Column(name = "description")
      public String getDescription() {
          return description;
      }
      public void setDescription(String description) {
          this.description = description;
      }
  
      @Id @GeneratedValue
      @Column(name = "id")
      public int getId() {
          return id;
      }
      public void setId(int id) {
          this.id = id;
      }
  }
  </code></pre>
  
  <p>I've already tried adding </p>
  
  <pre><code>&lt;bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"&gt;&lt;/bean&gt;
  &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
          &lt;property name="messageConverters"&gt;
            &lt;list&gt;
              &lt;ref bean="jsonConverter"/&gt;
            &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>to my <strong>dispatcher-servlet.xml</strong> or changing <em>jakcson-all</em> to <em>jackson-asl</em> and <em>jackson-core-asl</em> but output was the same.</p>