title: Spring Boot @RequestBody bind null values to a default value?
body: <p>I have a Spring Boot service that uses a custom POJO class for a POST request body. When client passes one of the fields as null, I would like that value to be set to 0.</p>
  <pre><code>@Data
  public class CardAnswer {
      private Integer answerId = 0; //should always be 0 and never set to null
      private String value;
  }
  </code></pre>
  <p>Used by the following controller:</p>
  <pre><code>@RequestMapping(value = &quot;/{category}&quot;, method = RequestMethod.POST)
      public SalesPulseMResponse updateSecurity(@PathVariable(value = SalesedgeConstants.CATEGORY)
                                                @RequestBody CardAnswer requestBody) {
  
          return fooService.updateFieldsByCategory(category, requestBody);
      }
  </code></pre>
  <p>Spring boot will handle the data binding from JSON to the <code>CardAnswer</code> for me, but whenever <code>answerId</code> is passed as null, I want it to retain 0. Is there a proper way to do that or would I have to parse through the class and set the values myself? Currently, if the client doesn't pass in this field in their request JSON, it will retain the default value, however, I want that default value to be retained even for null values that were passed in.</p>