title: How to distinguish between null and not provided values for partial updates in Spring Rest Controller
body: <p>I'm trying to distinguish between null values and not provided values when partially updating an entity with PUT request method in Spring Rest Controller.</p>
  
  <p>Consider the following entity, as an example:</p>
  
  <pre><code>@Entity
  private class Person {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      /* let's assume the following attributes may be null */
      private String firstName;
      private String lastName;
  
      /* getters and setters ... */
  }
  </code></pre>
  
  <p>My Person repository (Spring Data):</p>
  
  <pre><code>@Repository
  public interface PersonRepository extends CrudRepository&lt;Person, Long&gt; {
  }
  </code></pre>
  
  <p>The DTO I use:</p>
  
  <pre><code>private class PersonDTO {
      private String firstName;
      private String lastName;
  
      /* getters and setters ... */
  }
  </code></pre>
  
  <p>My Spring RestController:</p>
  
  <pre><code>@RestController
  @RequestMapping("/api/people")
  public class PersonController {
  
      @Autowired
      private PersonRepository people;
  
      @Transactional
      @RequestMapping(path = "/{personId}", method = RequestMethod.PUT)
      public ResponseEntity&lt;?&gt; update(
              @PathVariable String personId,
              @RequestBody PersonDTO dto) {
  
          // get the entity by ID
          Person p = people.findOne(personId); // we assume it exists
  
          // update ONLY entity attributes that have been defined
          if(/* dto.getFirstName is defined */)
              p.setFirstName = dto.getFirstName;
  
          if(/* dto.getLastName is defined */)
              p.setLastName = dto.getLastName;
  
          return ResponseEntity.ok(p);
      }
  }
  </code></pre>
  
  <p><strong>Request with missing property</strong></p>
  
  <pre><code>{"firstName": "John"}
  </code></pre>
  
  <p><em>Expected behaviour: update <code>firstName= "John"</code> (leave <code>lastName</code> unchanged).</em></p>
  
  <p><strong>Request with null property</strong></p>
  
  <pre><code>{"firstName": "John", "lastName": null}
  </code></pre>
  
  <p><em>Expected behaviour: update <code>firstName="John"</code> and set <code>lastName=null</code>.</em></p>
  
  <p>I cannot distinguish between these two cases, since<code>lastName</code> in the DTO is always set to <code>null</code> by Jackson.</p>
  
  <p><em>Note:
  I know that REST best practices (RFC 6902) recommend using PATCH instead of PUT for partial updates, but in my particular scenario I need to use PUT.</em></p>