title: Spring 3.x JSON status 406 "characteristics not acceptable according to the request "accept" headers ()"
body: <p>Upon trying to get my response in <code>JSON</code> using <code>Spring 3.x</code>, I get the <code>406 error</code> "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the <code>request</code> "accept" headers ()."</p>
  
  <p>Here is my environment</p>
  
  <pre><code>* Spring 3.2.0.RELEASE
  * included jackson-mapper-asl-1.7.9.jar, jackson-core-asl-1.7.9.jar
  * Tomcat 6.x
  * mvc:annotation-driven in Spring configuration XML file
  </code></pre>
  
  <p>My Controller: </p>
  
  <pre><code>@RequestMapping("/contest")
  public class ContestController {
  
      @RequestMapping(value="{name}", headers="Accept=*/*", method = RequestMethod.GET)
      public @ResponseBody Contest getContestInJSON(@PathVariable String name) {
          Contest contest = new Contest();
          contest.setName(name);
          contest.setStaffName(new String("contestitem1"));
  
          return contest;
      }
  
  }
  </code></pre>
  
  <p>My Spring Configuration file</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-3.0.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;context:component-scan base-package="com.contestframework.controllers" /&gt;
  
  &lt;bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"&gt;
  
  &lt;property name="mediaTypes"&gt;
      &lt;map&gt;
        &lt;entry key="atom" value="application/atom+xml"/&gt;
        &lt;entry key="html" value="text/html"/&gt;
        &lt;entry key="json" value="application/json"/&gt;
      &lt;/map&gt;
   &lt;/property&gt;
  
   &lt;property name="viewResolvers"&gt;
   &lt;list&gt;
    &lt;bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/&gt;
    &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="prefix" value="/WEB-INF/pages/"/&gt;
      &lt;property name="suffix" value=".jsp"/&gt;
    &lt;/bean&gt;
   &lt;/list&gt;
   &lt;/property&gt;
  
   &lt;property name="defaultViews"&gt;
    &lt;list&gt;
     &lt;bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /&gt;
    &lt;/list&gt;
   &lt;/property&gt;
  
   &lt;/bean&gt;
  
  &lt;mvc:annotation-driven /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>After this I just access the Controller using below:</p>
  
  <pre><code>http://domain/SpringWebProject/json/contest/abcd
  </code></pre>
  
  <p>and the response I get is 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>I also tried an alternate mechanism by access this using <code>Javascript</code> <code>AJAX</code> to make sure my request header has <code>application/JSON</code> but this led to the same Status 406 result</p>
  
  <pre><code>$.getJSON('contest/abcd', function(data) {
  console.log(data) }
  </code></pre>
  
  <p>Here is my REQUEST HEADER captured from browser:</p>
  
  <pre><code>Request URL:http://localhost:8080/SpringWebProject/json/contest/abcd
  Request Method:GET
  Status Code:406 Not Acceptable
  
  Accept:application/json, text/javascript, */*; q=0.01
  Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
  Accept-Encoding:gzip,deflate,sdch
  Accept-Language:en-US,en;q=0.8
  Connection:keep-alive
  Cookie:JSESSIONID=59689C95B0B9C21494EB0AB9D9F7BCCD
  Host:localhost:8080
  Referer:http://localhost:8080/SpringWebProject/json/welcome
  User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4
  X-Requested-With:XMLHttpRequest
  Response Headersview source
  Content-Length:1070
  Content-Type:text/html;charset=utf-8
  Date:Fri, 12 Oct 2012 18:23:40 GMT
  Server:Apache-Coyote/1.1
  </code></pre>
  
  <p>Appreciate any help in this regard.</p>