title: Spring REST @RequestBody is always empty
body: <p>So after I fixed the '415 media not supported' error (<a href="https://stackoverflow.com/questions/34067101/ajax-request-to-spring-rest-api-415-error/34083632#34083632">415 media not supported</a>), I encountered a new issue. My <code>RequestBody</code> is always empty. When I send a request via <strong>Chrome PostMan</strong> I always get a serialized entity back with only null values. I am using <code>Spring (4.2.3.RELEASE)</code>,<code>Jackson-databind (2.6.3)</code> and <code>jackson-core (2.6.3)</code> in my project. I am using annotation based configuration in my project (<code>@EnableWebMvc</code> to make spring automatically discover  <code>HTTPMessageConverters</code>).</p>
  
  <hr>
  
  <p><strong>Other posts</strong></p>
  
  <p>I am aware of other posts on stackoverflow, with almost the same problem. Yet they did not provide me with an answer. Also the most of the posts are for older Spring versions (pre 4.0), so some things are quite different now.  </p>
  
  <p>Similar posts:</p>
  
  <p><a href="https://stackoverflow.com/questions/27063392/requestbody-is-always-empty-in-spring">@RepsonseBody is always empty in Spring</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5806724/springs-requestbody-providing-empty-string-on-post">Spring's @RequestBody providing empty string on POST</a></p>
  
  <p><a href="https://stackoverflow.com/questions/16467035/spring-mvc-dont-deserialize-json-request-body">Using HttpServletRequest request</a></p>
  
  <hr>
  
  <p><strong>Spring controller</strong></p>
  
  <p>In my Spring <code>@RestController</code> I have the following code:</p>
  
  <pre><code>@RequestMapping(value = "/location/update/{id}", method = RequestMethod.PUT)
  public UserLocation updateUserLocation(@PathVariable("id") int id, UserLocation user) {        
          return user;        
  }
  </code></pre>
  
  <p>I am using a separate model class (<code>UserLocation</code>) for my data binding. This is because this way I have more control of the data my API sends and recieves.</p>
  
  <hr>
  
  <p><strong>Databinding class (UserLocation)</strong></p>
  
  <p>The UserLocation class consits of 3 properties with a constructor and the required getters and setters. (I could make those properties public, but I first want to fix this problem).</p>
  
  <pre><code>public class UserLocation {
  
      private Float latitude;
  
      private Float longitude;
  
      private Date lastActive;
  }
  </code></pre>
  
  <hr>
  
  <p><strong>JSON body</strong></p>
  
  <p>Via my AngularJS Ajax call (<code>$http.PUT</code>) I make a call to the spring controller with the following data: </p>
  
  <pre><code> {
      "latitude": 52.899370,
      "longitude": 5.804548,
      "lastActive": 1449052628407
   }
  </code></pre>
  
  <hr>
  
  <p><strong>PostMan request</strong></p>
  
  <p>I am developing a Cordova application, so to test requests without the need of building my application to my mobile phone, I am using <strong>Chrome PostMan Plugin</strong>. </p>
  
  <p>I am making the following call:</p>
  
  <p><code>URL:</code> <code>http://server:port/app/location/update/1</code></p>
  
  <p><code>Method:</code> <code>PUT</code></p>
  
  <p><code>Headers:</code> <code>Content-Type: application/json</code></p>
  
  <p><code>Body:</code></p>
  
  <pre><code>{
      "latitude": 52.899370,
      "longitude": 5.804548,
      "timestamp": 1449052628407
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Request result</strong></p>
  
  <p>With the request I get the following result:</p>
  
  <pre><code>{"latitude":null,"longitude":null,"lastActive":null}
  </code></pre>
  
  <p>This means that Spring does make a new instance of my <code>UserLocation</code> class, but it does not fill it with the data from the body.. </p>
  
  <hr>
  
  <p><strong>Spring PUT method</strong></p>
  
  <p>When using the PUT method in a Spring controller, isn't the entity updated immediately? So that would mean that there would be no extra logic in the controller for updating the entity right? (if the entity is of course a <strong>Hibernate/JPA</strong> model, which can be updated).</p>
  
  <hr>
  
  <p><strong><em>I cannot seem to figure out the problem. Anyone knows what I am doing wrong?</em></strong></p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>Adding <code>@RequestBody</code> to my controller code:</p>
  
  <pre><code>@RequestMapping(value = "/location/update/{id}", method = RequestMethod.PUT)
  public UserLocation updateUserLocation(@PathVariable("id") int id, @RequestBody UserLocation user) {        
              return user;        
  }
  </code></pre>
  
  <p>Brings me back to my original question (<a href="https://stackoverflow.com/questions/34067101/ajax-request-to-spring-rest-api-415-error/34083632#34083632">415 media not supported</a>). Adding this throws a 415 Media not supported error I cannot seem to fix.</p>
  
  <hr>
  
  <p><strong>Fixed. Solution below</strong></p>