title: Springboot: Ignore null values in response optionally
body: <p>There's a model class used by multiple APIs wherein if fields in this model class is null it is returned as null in the JSON response. However, another API is using the same model class and here the null fields should be omitted in the response.</p>
  <p>Is there a way to achieve this without creating another copy of the model file and just to ingore the null fields with <code>@JsonInclude(Include.NON_NULL)</code></p>
  <p>Example,</p>
  <pre><code>class A {
   private String b;
   private String c;
   private String d;
   // getters and setters
  }
  </code></pre>
  <p>Response 1 needed</p>
  <pre><code>&quot;A&quot;: {
    &quot;b&quot;: &quot;val1&quot;,
    &quot;c&quot;: null,
    &quot;d&quot;: &quot;val2&quot;
  }
  </code></pre>
  <p>Response 2 needed (omit values that are null)</p>
  <pre><code>&quot;A&quot;: {
    &quot;b&quot;: &quot;val1&quot;,
    &quot;d&quot;: &quot;val2&quot;
  }
  </code></pre>
  <p>Is there a way to achieve these behaviors using a single model class? Some sort of config while instantiation or something?</p>