title: Spring Partial Update Object Data Binding
body: <p>We are trying to implement a special partial update function in Spring 3.2. We are using Spring for the backend and have a simple Javascript frontend. I've not been able to find a straight-forward solution to our requirements, which is <em>The update() function should take in any number of field:values and update the persistence model accordingly.</em></p>
  
  <p>We have in-line editing for all of our fields, so that when the user edits a field and confirms, an id and the modified field get passed to the controller as json. The controller should be able to take in any number of fields from the client (1 to n) and update only those fields.</p>
  
  <p>e.g., when a user with id==1 edits his displayName, the data posted to the server looks like this:</p>
  
  <pre><code>{"id":"1", "displayName":"jim"}
  </code></pre>
  
  <p>Currently, we have an incomplete solution in the UserController as outlined below:</p>
  
  <pre><code>@RequestMapping(value = "/{id}", method = RequestMethod.POST )
  public @ResponseBody ResponseEntity&lt;User&gt; update(@RequestBody User updateUser) {
      dbUser = userRepository.findOne(updateUser.getId());
      customObjectMerger(updateUser, dbUser);
      userRepository.saveAndFlush(updateUuser);
      ...
  }
  </code></pre>
  
  <p>The code here works, but has some issues: The <code>@RequestBody</code> creates a new <code>updateUser</code>, fills in the <code>id</code> and the <code>displayName</code>. <code>CustomObjectMerger</code> merges this <code>updateUser</code> with the corresponding <code>dbUser</code> from the database, updating the only fields included in <code>updateUser</code>.</p>
  
  <p>The problem is that Spring populates some fields in <code>updateUser</code> with default values and other auto-generated field values, which, upon merging, overwrites valid data that we have in <code>dbUser</code>. Explicitly declaring that it should ignore these fields is not an option, as we want our <code>update</code> to be able to set these fields as well.</p>
  
  <p>I am looking into some way to have Spring automatically merge ONLY the information explicitly sent into the <code>update()</code> function into the <code>dbUser</code> (without resetting default/auto field values). Is there any simple way to do this?</p>
  
  <p><strong>Update:</strong> I've already considered the following option which does almost what I'm asking for, but not quite. The problem is that it takes update data in as <code>@RequestParam</code> and (AFAIK) doesn't do JSON strings:</p>
  
  <pre><code>//load the existing user into the model for injecting into the update function
  @ModelAttribute("user")
  public User addUser(@RequestParam(required=false) Long id){
      if (id != null) return userRepository.findOne(id);
      return null;
  }
  ....
  //method declaration for using @MethodAttribute to pre-populate the template object
  @RequestMapping(value = "/{id}", method = RequestMethod.POST )
  public @ResponseBody ResponseEntity&lt;User&gt; update(@ModelAttribute("user") User updateUser){
  ....
  }
  </code></pre>
  
  <p>I've considered re-writing my <code>customObjectMerger()</code> to work more appropriately with JSON, counting and having it take into consideration only the fields coming in from <code>HttpServletRequest</code>. but even having to use a <code>customObjectMerger()</code> in the first place feels hacky when spring provides <em>almost exactly what I am looking, minus the lacking JSON functionality</em>. If anyone knows of how to get Spring to do this, I'd greatly appreciate it!</p>