title: Using ResponseBody annotation in Spring for returning a Json not working
body: <p>I am working on a spring mvc web application, in which I am using Google Visualization API for generating some charts. I have a model class which contains 2 arraylists, which represent the data that I`m sending to the function that draws the chart (this is what i want to be converted to a JSON).</p>
  
  <p>The model class: </p>
  
  <pre class="lang-java prettyprint-override"><code>@Component
  public class JsonResponse {
  
  private List&lt;Integer&gt; percentages = new ArrayList&lt;Integer&gt;();
  private List&lt;String&gt; topics = new ArrayList&lt;String&gt;();
  
  public JsonResponse(){
  }
  
  public List&lt;Integer&gt; getPercentages() {
      return percentages;
  }
  
  public void setPercentages(List&lt;Integer&gt; percentages) {
      this.percentages = percentages;
  }
  
  public List&lt;String&gt; getTopics() {
      return topics;
  }
  
  public void setTopics(List&lt;String&gt; topics) {
      this.topics = topics;
  }
  }
  </code></pre>
  
  <p>Then I<code>ve got a</code>@Component` annotated class which contains a method that returns a model object (of the class that I wrote above), with the 2 arraylists attributes populated.</p>
  
  <pre class="lang-java prettyprint-override"><code>@Component
  public class ChartUtils {
      @Autowired
      public JsonResponse response;
  
      public JsonResponse listPieChartData( ModelAndView model ,int waveId ){
  
      //arraylists for chart generation
  List&lt;Integer&gt; percentages = new ArrayList&lt;Integer&gt;();
  List&lt;String&gt; topics = new ArrayList&lt;String&gt;();
  
      {... code for accessing the DB and processing some data and then populating the 2                  
       arraylists ... }
  
  response.setTopics(topics);
  response.setPercentages(percentages);
  
  return response;}
  }
  </code></pre>
  
  <p>So the Controller class, the one that has the mapping for the action that I am calling to gather data for the chart generation and in which I am calling <code>listPieChartData</code> method, from the class above, and in which I'm also using the <code>@ResponseBody</code> annotation is this:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Controller
  public class ChartController {
  
  @Autowired
  public ChartUtils utils;
  
  @Autowired
  public JsonResponse response;
  
  @RequestMapping(value = "/drawPieChart", method = RequestMethod.GET )
  @ResponseBody
  public JsonResponse drawPieChart( ModelAndView model,
          @RequestParam(value = "id", defaultValue = "-1") int waveId ) {
  
      return utils.listPieChartData(model,waveId ); }
  </code></pre>
  
  <p>The JavaScript function that draws the chart :</p>
  
  <pre class="lang-js prettyprint-override"><code>function drawColumnChart(percentages, topics , div,width,height) {
      var data = new google.visualization.DataTable();
  
  data.addColumn('string', 'Wave');
  for (var i=0; i &lt; topics.length; i++){
      data.addColumn( 'number', topics[i] ); 
  }
  
      data.addRow( percentages );
  
      var wave=percentages[0];
      var options = {
          'title':'Generated Chart For '+wave,
          'backgroundColor': { fill: "none" },
          'is3D': true,
          'width':width,
          'height':height,
      };
  
  var chart = new google.visualization.ColumnChart(document.getElementById(div));
  chart.draw(data, options);
  </code></pre>
  
  <p>}</p>
  
  <p>And the AJAX call to the controller's mapped method (for gathering data) that finally calls the above JS function to obtain the chart (I'm also sending the request param int id for the controller method , I didn't wrote that)</p>
  
  <pre class="lang-js prettyprint-override"><code>$("#button").live("click", function(){
  
  var arrayP, arrayT;
  $.ajax({
      url: "drawPieChart",      
  contentType: "application/json",
  data: params,
  success: function(data) {
      $.each(data, function(messageIndex, message) {
      if (messageIndex === 0) {
              arrayP = message;
      } else {
      arrayT = message;
      }
          });
      drawPieChart(arrayP, arrayT,'chart_div',600,400);
      }
      });  
  });
  </code></pre>
  
  <p>I know this is a lot of code :) but it's pretty simple code, to understand the flow better, here is how it`s working:</p>
  
  <p>From a button input I'm calling, with AJAX, the mapped method to the drawPieChart action (which is in the <code>ChartController</code> class), this methods sends the response through invoking the listPieChart method (from the <code>ChartUtils</code> class), which returns a <code>JsonResponse</code> object, (which contains 2 arraylists). This <code>JsonResponse</code> should be converted to a JSON, because in the AJAX request, I'm telling that the request needs a JSON input (via contentType: "application/json"), and it should get it because I use <code>@ResponseBody</code> in the controller method mapped for this request.</p>
  
  <p>I`m getting this response: </p>
  
  <blockquote>
    <p>The resource identified by this request is only capable of generating
    responses with characteristics not acceptable according to the request
    "accept" headers ().<br>
    (HTTP Status 406)</p>
  </blockquote>
  
  <p>Please correct me where I'm wrong, I just can't get this working and I can't figure out why...</p>
  
  <p>And my <code>servlet-context.xml</code>:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc       
  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  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"&gt;
  
    &lt;!-- DispatcherServlet Context: defines this servlet's request-processing      
  infrastructure --&gt;
  
    &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
    &lt;annotation-driven /&gt;
  
    &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving up
        static resources in the ${webappRoot}/resources directory --&gt;
    &lt;resources mapping="/resources/**" location="/resources/" /&gt;
  
    &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources
      in the /WEB-INF/views directory --&gt;
    &lt;beans:bean 
       class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt;
      &lt;beans:property name="suffix" value=".jsp" /&gt;
    &lt;/beans:bean&gt;
  
    &lt;context:component-scan base-package="com.bla.bla" /&gt;
  
    &lt;beans:import resource="classpath:springJDBC.xml" /&gt;
  
  &lt;/beans:beans&gt;
  </code></pre>