title: Spring MVC : How to populate my Object with JSON
body: <p>I have an update method (AJAX service) that expect an object. This object is populate by JSon and I want to test it but I can't..</p>
  
  <p>I use spring mvc to set up my service.</p>
  
  <p>My Service : </p>
  
  <pre><code>    @RequestMapping(value="texte/updateMyObject", method= RequestMethod.POST, consumes = "application/json;charset=UTF-8")
      public void updateMyObject(MyObject myObject) throws IOException {
  
          logger.info("UPDATE !!!");
  
      }
  </code></pre>
  
  <p>My Object : </p>
  
  <pre><code>public class MyObject {
  private Integer id;
  private String name;
  
  public MyObject(){}
  
  // getter &amp; setter
  
  }
  </code></pre>
  
  <p>I use Postman (chrome tool) to test my service : <a href="https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm" rel="nofollow noreferrer">https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm</a></p>
  
  <p>I think I have to send the good JSON but I don't know which one.</p>
  
  <p>For the moment I have test this pattern : </p>
  
  <pre><code>"MyObject" : {"id" : "1", "name" : "test"}
  </code></pre>
  
  <p>or 
      {"id" : "1", "name" : "test"}</p>
  
  <p><strong>Edit 1 :</strong> </p>
  
  <p>When my service is call, the fields of myObject are all null.</p>
  
  <p><strong>Edit 2 :</strong> </p>
  
  <p>I play this kind of HTTP request : </p>
  
  <pre><code>POST /myapp/texte/updateMyObject.sp HTTP/1.1
  Host: localhost:8080
  Content-Type: application/json; charset=UTF-8
  Cache-Control: no-cache
  
  {"id" : "1", "name" : "test"}
  </code></pre>
  
  <p><strong>Edit 3 :</strong> </p>
  
  <p>My config spring in my web.xml. If you need my context ask me.</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;springMvc&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;classpath*:spring/applicationContext-springmvc.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;springMvc&lt;/servlet-name&gt;
      &lt;url-pattern&gt;*.sp&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p><strong>Edit 4 :</strong> 
  I made the following test to show you that my Object is not populated.</p>
  
  <p><img src="https://i.stack.imgur.com/3NT1H.jpg" alt="enter image description here"></p>