title: Why Doesn't Setting Jackson to Accept Empty Lists as Null Work?
body: <p>My company has a Java web service using Spring Web that accepts JSON via a REST API. We're using Maven and our Jackson version is 2.9. We're trying to prevent deserialization exceptions from being thrown when an integrator passes in an empty list when our API isn't expecting one.</p>
  
  <p>For example, here's my application class:</p>
  
  <pre><code>import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  
  @EnableWebMvc
  @SpringBootApplication
  public class ExampleApplication extends SpringBootServletInitializer {
  
      public static void main(String[] args) {
          SpringApplication.run(ExampleApplication.class, args);
      }
  }
  </code></pre>
  
  <p>Student.java class:</p>
  
  <pre><code>import lombok.Data;
  import java.util.Map;
  
  @Data
  public class Student {
  
      private String firstName;
      private String lastName;
      private Map&lt;String, String&gt; classGrades;
  }
  </code></pre>
  
  <p>A StudentController:</p>
  
  <pre><code>package com.example.example.controllers;
  
  import com.example.example.models.Student;
  import org.springframework.http.RequestEntity;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;
  
  import java.util.stream.Stream;
  
  @RestController()
  @RequestMapping(value = "/Students/", produces = "application/json")
  public class StudentController {
  
      @PostMapping(path = "")
      public RequestEntity&lt;Student&gt; createNewStudent(@RequestBody Student student) {
  
         return null;
      }
  }
  </code></pre>
  
  <p>The application.properties:</p>
  
  <pre><code>spring.jackson.deserialization.accept-empty-array-as-null-object=true
  </code></pre>
  
  <p>The pom.xml contains all the default dependencies, with the following added:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
      &lt;artifactId&gt;lombok&lt;/artifactId&gt;
      &lt;version&gt;1.18.10&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Every other project file is default to the project structure generated by the Spring initializer. The expected request body (JSON formed using Postman):</p>
  
  <pre><code>{
    "firstName": "Mark",
    "lastName": "Twain",
    "classGrades": {
  
    }
  }
  </code></pre>
  
  <p>Works just fine. However, if any field (though in our specific case, the <code>classGrades</code> field) receives an empty list, a Jackson deserialization exception is thrown. An example JSON request that fails:</p>
  
  <pre><code>{
    "firstName": "Mark",
    "lastName": "Twain",
    "classGrades": []
  }
  </code></pre>
  
  <p>And the exception that is thrown:</p>
  
  <pre><code>org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.util.LinkedHashMap` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.LinkedHashMap` out of START_ARRAY token
   at [Source: (PushbackInputStream); line: 65, column: 28] 
  </code></pre>
  
  <p>According to the project's <a href="https://github.com/FasterXML/jackson-databind/wiki/Deserialization-Features" rel="nofollow noreferrer">github</a> page, the option ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT should resolve this issue for us. We've tried setting this directly on an <code>ObjectMapper</code> configuration object and within <code>application.properties</code> using the line:</p>
  
  <pre><code>spring.jackson.deserialization.accept-empty-array-as-null-object=true
  </code></pre>
  
  <p>Neither method seemed to take effect. We're currently using a workaround using the <code>@JsonDeserialize(using = MyCustomDeserializer.class)</code> annotaion on fields prone to this problem. However, we would like to be able to have all of our fields treat empty lists as null by default.</p>
  
  <p>Are we misunderstanding the configuration option and using it incorrectly? Is there a way to treat empty lists as null within our app, and if so, how can we accomplish this?</p>