title: Bound a JSON object to JavaBean class in Spring MVC, I have some things unclear
body: <p>in this period I am studying the Spring MVC showcase example.</p>
  
  <p>About the request mapping I have a litle doubt, I have the following situation:</p>
  
  <p>In a view I submit a form, submitting this form start the following 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 submit (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 tath 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>So in my case is generated the following JavaScript Object having JSON format:</p>
  
  <p><strong>{ \"foo\": \"bar\", \"fruit\": \"apple\" }</strong></p>
  
  <p>Ok, then my Jquery function do an AJAX call and send my JSON object to the Spring controller class.</p>
  
  <p>This is the method that handles this HTTP Request:</p>
  
  <pre><code>@RequestMapping(value="/mapping/consumes", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody String byConsumes(@RequestBody JavaBean javaBean) {
      System.out.println("foo: " + javaBean.getFoo() + " fruit: " + javaBean.getFruit());
      return "Mapped by path + method + consumable media type (javaBean '" + javaBean + "')";
  }
  </code></pre>
  
  <p>This method handles HTTP POST Request towards the URL: "/mapping/consumes" and consumes a JSON object (this is specify by <strong>consumes=MediaType.APPLICATION_JSON_VALUE</strong>)</p>
  
  <p>Ok this method take an input parameter that is annoted using the @RequestBody annotation that indicates that my method parameter should be bound to the value in the body of the HTTP Request (my JSON object)</p>
  
  <p>JavaBean class contain only 2 property that are:</p>
  
  <pre><code>private String foo;
      private String fruit;
  </code></pre>
  
  <p>(as the JSON object properties)</p>
  
  <p>To bound the JSON object properties with the JavaBean propertis, the JavaBean class is annoted by the <strong>@XmlRootElement</strong> annotation:</p>
  
  <p>package org.springframework.samples.mvc.mapping;</p>
  
  <p>import javax.xml.bind.annotation.XmlRootElement;</p>
  
  <pre><code>@XmlRootElement
  public class JavaBean {
  
  private String foo = "bar";
      private String fruit = "apple";
  
      // Setter and Getter method for properties
  </code></pre>
  
  <p>Now I know that when you see an @RequestBody annotation on an object in a Spring controller, and the consumes attribute of the @RequestMapping annotation is "application/xml" (or pre spring 3.1 the Content-Type header is set to "application/xml") this will tell Spring that the payload of the request message is XML and needs to be unmarshalled to the object that was annotated with @RequestBody. In this case the object that was annotated with @RequestBody will typically be annotated itself with JAXb annotations to provide the meta data needed to marshal and unmarshal that object to/from XML, and @XmlRootElement is one of those annotations providing that metadata. </p>
  
  <p>Ok, this is all clear for me...my only doubt is related to the fact that in my <strong>@RequestMapping</strong> annotation I have not that the <strong>Content-Type header</strong> is set to <strong>"application/xml</strong> but I have: <strong>consumes=MediaType.APPLICATION_JSON_VALUE</strong></p>
  
  <p>So I have the following question for you:</p>
  
  <p>1) What is the difference between <strong>Content-Type header</strong> and <strong>consumes=MediaType.APPLICATION_JSON_VALUE</strong></p>
  
  <p>2) From what I understand reading the ** @XmlRootElement** documentation: <a href="http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlRootElement.html" rel="nofollow">http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlRootElement.html</a></p>
  
  <p>this annotation provides two-way association between XML content and content JAVA.
  So I can map the content of XML fields in Java class properties, and conversely</p>
  
  <p>The problem is thath in this case I have not an XML "object" but I am using a JSON object that is an alternative to XML !!! What am I missing?</p>
  
  <p>Thank you very much</p>
  
  <p>Andrea</p>