title: HTTP PUT to update database using partial Json - Spring, Postgres
body: <p>I am writing a web api using Spring and Postgres. </p>
  
  <ul>
  <li><p>I have a case where I take a Json object <code>item</code></p></li>
  <li><p>The uri is <code>/api/item/{itemId}</code></p></li>
  <li><p>Request type is <code>PUT</code></p></li>
  </ul>
  
  <p>Json: </p>
  
  <pre><code>{
     "name":"itemname",
     "description":"item description here"
  }
  </code></pre>
  
  <p>So I do (using JdbcTemplate) a <code>SELECT</code> statement to check if the <code>itemId</code> exists and then update if it does.</p>
  
  <p>I would also like to implement a case with partial puts taking Json that look like this::</p>
  
  <pre><code>{
     "name":"itemname"
  }
  </code></pre>
  
  <p>OR</p>
  
  <pre><code>{
     "description":"item description here"
  }
  </code></pre>
  
  <p>where only the respective fields are updated. In Spring, the variables not present are automatically <code>null</code>. </p>
  
  <p>The way this is implemented now is:</p>
  
  <ol>
  <li><code>SELECT</code> all columns from the <code>items</code> table</li>
  <li>Sequentially check every single expected variable for null and if they are null, replace the null with the value selected from the table in step 1. </li>
  <li><code>UPDATE</code> all columns with the values (none of which should be <code>null</code> if the table has a <code>not null</code> constraint)</li>
  </ol>
  
  <p>Question: How do you do this without <code>== null or != null</code> checks? Is seems to be poor design and involves iterating through every single expected variable for every single <code>PUT</code> request (I will have many of those).</p>
  
  <p>Desired responses (in order of desirability):</p>
  
  <ol>
  <li>There's a way in Postgres where if a <code>null</code> value is input, the column-value is simply not written to the database (and no error is produced)</li>
  <li>There is a way to use Spring (and Jackson) to create a Java object with only the provided values and a way to generate SQL and JdbcTemplate code that only updates those specific columns.</li>
  <li>Patch is the way of life - implement it</li>
  <li>Change the front-end to always send everything</li>
  </ol>