title: @RestController throws HTTP Status 406
body: <p>I am working on a basic Hello World program using Spring and Restful webservices. But when I try to call my service I am getting below error message:</p>
  
  <blockquote>
    <p><strong>HTTP Status 406 -</strong> </p>
    
    <p>description - The resource identified by this request is only capable
    of generating responses with characteristics not acceptable according
    to the request "accept" headers.</p>
  </blockquote>
  
  <p>Here is my web.xml file's servlet mapping:</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;mvc-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;mvc-dispatcher&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>Here is my spring web 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.xsd
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc.xsd"&gt;
  
      &lt;context:component-scan base-package="hello" /&gt;
  
      &lt;mvc:annotation-driven /&gt;
  
      &lt;!-- Map returned view name "rssViewer" to bean id "rssViewer" 
      &lt;bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /&gt;
          --&gt;
      &lt;bean id="xmlViewer" 
          class="org.springframework.web.servlet.view.xml.MarshallingView"&gt;
          &lt;constructor-arg&gt;
              &lt;bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller"&gt;
                  &lt;property name="classesToBeBound"&gt;
                      &lt;list&gt;
                          &lt;value&gt;hello.Greeting&lt;/value&gt;
                      &lt;/list&gt;
                  &lt;/property&gt;
              &lt;/bean&gt;
          &lt;/constructor-arg&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>Here is my Maven pom.xml file:</p>
  
  <pre><code>&lt;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"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.examples&lt;/groupId&gt;
      &lt;artifactId&gt;WebServices1&lt;/artifactId&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;WebServices1 Maven Webapp&lt;/name&gt;
      &lt;url&gt;http://maven.apache.org&lt;/url&gt;
      &lt;properties&gt;
          &lt;spring.version&gt;4.1.6.RELEASE&lt;/spring.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- for compile only, your container should have this --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
              &lt;version&gt;2.5&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-oxm&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &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;/dependencies&gt;
      &lt;build&gt;
          &lt;finalName&gt;WebServices1&lt;/finalName&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>Here is my controller:</p>
  
  <pre><code>package hello;
  
  import java.util.concurrent.atomic.AtomicLong;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.RestController;
  
  @RestController
  public class GreetingController {
  
      private static final String template = "Hello, %s!";
      private final AtomicLong counter = new AtomicLong();
  
      @RequestMapping("/greeting")
      public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
          return new Greeting(counter.incrementAndGet(),
                              String.format(template, name));
      }
  }
  </code></pre>
  
  <p>This is my Greeting pojo class:</p>
  
  <pre><code>package hello;
  
  public class Greeting {
  
      private final long id;
      private final String content;
  
      public Greeting(long id, String content) {
          this.id = id;
          this.content = content;
      }
  
      public long getId() {
          return id;
      }
  
      public String getContent() {
          return content;
      }
  }
  </code></pre>
  
  <p>When I deploy my application and access the URL as <code>http://localhost:8081/WebServices1/rest/greeting</code>, I am getting <strong>HTTP 406</strong> error message in browser.</p>
  
  <p><strong>update:</strong></p>
  
  <p>As per my mozilla firebug plugin, here are my headers for request:</p>
  
  <pre><code>Accept  
  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Accept-Encoding 
  gzip, deflate
  Accept-Language 
  en-US,en;q=0.5
  Cache-Control   
  max-age=0
  Connection  
  keep-alive
  Cookie  
  JSESSIONID=9B49E7A23D6CDE85CAC4152E22834D35; textwrapon=false; textautoformat=false; wysiwyg=textarea
  ; m=34e2:|2663:t|c01:t|5cf4:t|4a01:t|54e1:t
  Host    
  localhost:8081
  User-Agent  
  Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0
  </code></pre>
  
  <p><strong>Response Headers:</strong></p>
  
  <pre><code>Content-Language    
  en
  Content-Length  
  1067
  Content-Type    
  text/html;charset=utf-8
  Date    
  Mon, 22 Jun 2015 11:00:08 GMT
  Server  
  Apache-Coyote/1.1
  </code></pre>