title: HTTP Status 406. Spring MVC 4.0, jQuery, JSON
body: <p>I want to send JSON from my controller. I have the following configuration.</p>
  
  <p><strong>spring-servlet.xml :</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
             http://www.springframework.org/schema/mvc
             http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
      &lt;mvc:annotation-driven/&gt;
  
      &lt;mvc:resources mapping="/resources/**" location="/resources/"/&gt;
      &lt;context:component-scan base-package="com.castle.controllers"/&gt;
  
      &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&gt;
          &lt;property name="prefix" value="/views/"/&gt;
          &lt;property name="suffix" value=".jsp"/&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>.js :</strong> </p>
  
  <pre><code>function testAjax() {
      var data = {userName: "MyUsername", password:"Password"};
      $.ajax({
          url: 'ajax/test.htm',
          dataType : 'json',
          type : 'POST',
          contentType: "application/json",
          data: JSON.stringify(data),
          success: function(response){
              alert('Load was performed.');
          }
      });
  }
  </code></pre>
  
  <p><strong>UserTest.java:</strong></p>
  
  <pre><code>public class UserTest {
      private String userName;
      private String password;
  
      public String getUserName() {
          return userName;
      }
  
      public void setUserName(String userName) {
          this.userName = userName;
      }
  
      public String getPassword() {
          return password;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  }
  </code></pre>
  
  <p><strong>TestAjaxController.java :</strong> </p>
  
  <pre><code>@Controller
  @RequestMapping("/ajax")
  public class TestAjaxController {
  
      @RequestMapping(method = RequestMethod.POST, value = "/test.htm")
      public @ResponseBody
      UserTest testAjaxRequest(@RequestBody UserTest user) {
          return user;
      }
  }
  </code></pre>
  
  <p><strong>pom.xml :</strong> </p>
  
  <pre><code>    &lt;dependency&gt;
                  &lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt;
                  &lt;artifactId&gt;jackson-mapper-asl&lt;/artifactId&gt;
                  &lt;version&gt;1.9.13&lt;/version&gt;
              &lt;/dependency&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt;
                  &lt;artifactId&gt;jackson-core-asl&lt;/artifactId&gt;
                  &lt;version&gt;1.9.13&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>When i do this request, i get in my Controller JSON represented as UserTest object. But on return :</p>
  
  <p>HTTP Status 406 - The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.</p>
  
  <p>What i'm doing wrong? I know, there is a lot of questions about such cases, but i can't fix it for 2 days...</p>
  
  <p><strong>UPDATE</strong>
  I Have found the solution!!
  It's only need to return an Object. Not a User object or something. 
  But 
  <code>return Object;</code> </p>
  
  <pre><code> public @ResponseBody Object testAjaxRequest(@RequestBody UserTest user) {
          List&lt;UserTest&gt; list = new ArrayList&lt;&gt;();
          list.add(user);
          list.add(user);
          list.add(user);
          return list;
  </code></pre>