Configure Jackson to throw an exception when a field is missing

Unfortunately this is not supported by Jackson at this moment.

Solution could be to add validation in your constructor. As ideally if you don't want to have those values serialized as null's , it does mean you shouldn't have them as null's at all (constructed in other way). For example,

public class Person {
  private String name;
  public Person() {
     checkNotNull(name);
  }
} 

however this might not fittable in all situations, specially if you are using your object's other than through serializing/deserializing.

Though they have required attribute in @JsonProperty annotation, it is not supported during deserialization at all, and has been introduced only for decorating JSON schemas. See this topic


As of Jackson 2.6, there is a way, but it does not work on class attribute annotations, only constructor annotations:

/* DOES *NOT* THROW IF bar MISSING */
public class Foo {    
    @JsonProperty(value = "bar", required = true)
    public int bar;
}

/* DOES THROW IF bar MISSING */
public class Foo {
    public int bar;
    @JsonCreator
    public Foo(@JsonProperty(value = "bar", required = true) int bar) {
        this.bar = bar;
    }
}

Tags:

Jackson