title: In a java REST API, using PATCH vs PUT to update an entity
body: <p>I am about to start development on a new rest api in Java. 
  My question is about the use of PATCH - Why?</p>
  
  <p>Lets say, we have an entity named Address.java</p>
  
  <pre><code>public class Address {
  
      @Id
      private Long id
  
      @NotNull
      private String line1;
  
      private String line2;       //optional
  
      @NotNull
      private String city;
  
      @NotNull
      private String state;   
  }
  </code></pre>
  
  <p>To create a new Address, I would do this http request:</p>
  
  <pre><code>POST http://localhost:8080/addresses
  </code></pre>
  
  <p>with the following request:</p>
  
  <pre><code>{
      "line1" : "mandatory Address line 1",
      "line2" : "optional  Address line 2",
      "city"  : "mandatory City",
      "state" : "cd"
  }
  </code></pre>
  
  <p>Assume the record created has an id 1</p>
  
  <p>The corresponding @RestController AddressResource.java will have this method :</p>
  
  <pre><code>@PostMapping(value = "/addresses")
  public ResponseEntity&lt;Address&gt; create(@valid Address newAddress) {
      addressRepo.save(newAddress);
  }
  </code></pre>
  
  <p>@valid will ensure the entity is valid before storing the data into the table.</p>
  
  <p>Now assume, I move from my apartment above to a house down the street. If I use a PATCH, it becomes</p>
  
  <pre><code>PATCH http://localhost:8080/addresses/1
  </code></pre>
  
  <p>with request payload:</p>
  
  <pre><code>{
      "line1" : "1234 NewAddressDownTheStreet ST",
      "line2" : null
  }
  </code></pre>
  
  <p>The corresponding @RestController method would be :</p>
  
  <pre><code>@PatchMapping(value = "/addresses/{id}")
  public ResponseEntity&lt;Address&gt; patchAddress(@PathVariable Long id, Address partialAddress) 
  {
      Address dbAddress = addressRepo.findOne(id);
      if (partialAddress.getLine1() != null) {
          dbAddress.setLine1(partialAddress.getLine1());
      }
      if (partialAddress.getLine2() != null) {
          dbAddress.setLine2(partialAddress.getLine2());
      }
      if (partialAddress.getCity() != null) {
          dbAddress.setCity(partialAddress.getCity());
      }
      if (partialAddress.getState() != null) {
          dbAddress.setState(partialAddress.getState());
      }
  
      addressRepo.save(dbAddress)
  }
  </code></pre>
  
  <p>Now if you query the table, won't my address be ?</p>
  
  <pre><code>"line1" : "1234 NewAddressDownTheStreet ST",
  "line2" : "optional  Address line 2",       &lt;-- INCORRECT. Should be null.
  "city"  : "mandatory City",
  "state" : "cd"
  </code></pre>
  
  <p>As can be seen, the above updates results in an incorrect value for line2.
  This is because in java all instance variables in the Address class are initialized to null (or default initial values if they are primitives) when a class is instantiated. So there is no way to distinguish between line2 being changed to null from the default value.</p>
  
  <p><strong>Question 1) Is there a standard way to work around this?</strong></p>
  
  <hr>
  
  <p>Another disadvantage is that, I cannot use @Valid annotation to validate the request at the entry point - coz it is only a partial. So, invalid data could get into the system.</p>
  
  <p>For example, imagine there was additional field with the following definition:</p>
  
  <pre><code>@Min(0) 
  @Max(100)
  private Integer lengthOfResidencyInYears, 
  </code></pre>
  
  <p>And the user accidentally typed 190 (when they really meant 19 years), it would not fail.</p>
  
  <hr>
  
  <p>Instead of PATCH, if I had used PUT, the client would need to send the complete address object.
  This has the advantage that I can use @Valid to ensure that the Address is indeed valid </p>
  
  <hr>
  
  <p>If one makes the premise that a GET MUST always be done before doing any updates, why wouldn't one use PUT over PATCH? 
  Am I missing something?</p>
  
  <p><strong>Aside</strong></p>
  
  <p>My conclusion is that developers using dynamically typed languages are the proponents of using PATCH as I cannot see any benefit to using it from a statically typed language line Java or C#. It just seems to add more complexity.</p>