title: Representing undefined value in REST API request in Java 8 using Jackson
body: <p>I have a <code>PATCH</code> REST endpoint, exposing a JSON interface, which can be used to partially update an entity, based on the attributes which are actually sent in the body.</p>
  <p>Let's consider a sample class representing the entity:</p>
  <pre><code>class Account {
      private UUID accountUuid;
      private UUID ownerUuid;
      private String number;
      private String alias;
  
      // constructor, getters and setters omitted
  }
  </code></pre>
  <p>where <code>accountUuid</code>, <code>ownerUuid</code> and <code>number</code> are required properties, and <code>alias</code> is optional. Additionally I have a command class for updating said account:</p>
  <pre><code>class UpdateAccountCommand {
      private UUID accountUuid;
      private String number;
      private String alias;
  
      // constructor, getters and setters omitted
  }
  </code></pre>
  <p>For my <code>PATCH</code> endpoint, e.g. <code>PATCH /accounts/{account-uuid}</code>, I'd like to implement a functionality, that only properties actually sent in the request are changed:</p>
  <pre><code>// this should only update the account number
  {&quot;number&quot;: &quot;123456&quot;}
  
  // this should only update the alias, by setting it to null
  {&quot;alias&quot;: null}
  
  // this shouldn't update anything
  {}
  </code></pre>
  <p>For <strong>required</strong> properties, this is fairly easy to do. After deserialisation from the JSON string to <code>UpdateAccountCommand</code> instance using Jackson, I simply check if a required property is <code>null</code>, and when it's not <code>null</code>, I update the given property.</p>
  <p>However the situation complicates with <strong>optional</strong> properties, since the <code>alias</code> property is <code>null</code> in both cases:</p>
  <ul>
  <li>when the request body explicitly specifies the <code>alias</code> as null,</li>
  <li>when the <code>alias</code> property is not specified in the request body at all.</li>
  </ul>
  <p>How can I model these optional properties, so that I can indicate this <em>removable</em> mechanism?</p>