title: How to map JSON object in Spring MVC?
body: <p>I am studying the Spring MVS Showcase downloadable from di STS dashboard. I am studying how Spring maps the request and I am having some problem to understand the following thing:</p>
  
  <p>I have this form with a submit button:</p>
  
  <pre><code>        &lt;li&gt;
              &lt;form id="byConsumes" class="readJsonForm" action="&lt;c:url value="/mapping/consumes" /&gt;" method="post"&gt;
                  &lt;input id="byConsumesSubmit" type="submit" value="By consumes" /&gt;
              &lt;/form&gt;
          &lt;/li&gt;
  </code></pre>
  
  <p>When I click on the submit button a Jquery function that creates a JSON object is passed by the HTTP Post Request, this is the code of the JQuery function:</p>
  
  <pre><code>$("form.readJsonForm").submit(function() {          
  
      var form = $(this);                 // Variabile che si riferisce all'elemento nel DOM che ha scatenato l'evento click (il form) 
      var button = form.children(":first");       // Seleziona il bottone submit 
  
      var data = form.hasClass("invalid") ?       // OPERATORE CONDIZIONALE: il form ha classe "invalid" ? 
              "{ \"foo\": \"bar\" }" :            // SI: foo = bar 
              "{ \"foo\": \"bar\", \"fruit\": \"apple\" }";   // NO: foo= bar ; fruit = apple 
  
  
      /* AJAX CALL PARAMETER:
         type: Say to the servlet path, the request is a POST HTTP Request
         url: The address to which to send the call   
         data: the content of my data variable
         contentType: an object having JSON format
         dataType: the type of content returned by the server
      */
      $.ajax({ type: "POST", url: form.attr("action"), data: data, contentType: "application/json", dataType: "text", 
          success: function(text) { MvcUtil.showSuccessResponse(text, button); }, 
          error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }});
  
      return false;
  });
  </code></pre>
  
  <p>The JSON object that I have create and passed is represented by the data variable and contain the following key\value: <strong>{ \"foo\": \"bar\", \"fruit\": \"apple\" }</strong></p>
  
  <p>Something like:</p>
  
  <p><strong>foo: bar</strong></p>
  
  <p><strong>fruit: apple</strong></p>
  
  <p>Now, in my controller I have the method that handles this request:</p>
  
  <pre><code>@RequestMapping(value="/mapping/consumes", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody String byConsumes(@RequestBody JavaBean javaBean) {
      return "Mapped by path + method + consumable media type (javaBean '" + javaBean + "')";
  }
  </code></pre>
  
  <p>So it is clear for me that this method handles HTTP Post Request towards "mapping/consumes" path (only POST Request) but I am not sure about the meaning of the following items:</p>
  
  <ol>
  <li><p><strong>consumes=MediaType.APPLICATION_JSON_VALUE</strong> : what exactly does this mean? I think that it says to Spring that this method receives an object in JSON format, so it can be parsed in some way...but I am not sure about it and I have not found it in the documentation.</p>
  
  <p>What is <strong>consumes</strong>? a variable or something like an annotation? I am not understanding things because here it is a parameter of the @RequestMapping annotation but searching on Google shows it is used as a standalone annotation...</p></li>
  <li><p>In my byConsumes() method I have the following input parameter: <strong>@RequestBody JavaBean javaBean</strong>.
  Reading the Spring documentation I understand that: @RequestBody method parameter annotation indicates that using @RequestBody annotation a method parameter should be bound to the value of the HTTP request body.</p>
  
  <p>So in practice this thing mean that I have my JSON object inside my HTTP Request body field and using this annotation I am converting it in an object named javaBean having class JavaBean?</p>
  
  <p>If my affirmation is true...what kind of object is a JavaBean type object? an object that contains only a number of variables and the corresponding getter and setter methods? (in the previus case an object that contain only two variable: the first one named foo and having value "bar", the second one having name fruit and having value "apple")</p></li>
  </ol>
  
  <p>is it right?</p>
  
  <p>Thank you very much
  Andrea</p>