Jackson - Required property?

43,848

Solution 1

Jackson does not include validation functionality, and this is by design (i.e. that is considered out-of-scope). But what is usually used is Bean Validation API implementation. The nice thing about this is decoupling between data format handling, and validation logic. This is what frameworks like DropWizard use; and it's the direction JAX-RS (like Jersey) are taking things for JAX-RS 2.0.

Solution 2

You can mark a property as required with the @JsonProperty(required = true) annotation, and it will throw a JsonMappingException during deserialization if the property is missing or null.

Edit: I received a downvote for this without comment. I'd love to know why, since it does exactly the right thing.

Solution 3

If you want to make sure a json field is provided, you have to use the @JsonProperty(value = "fieldName", required = true) annotation as a parameter to the constructor. But this is not enough, also the Constructor should have @JsonCreator annotation.

For example, if you have a field named 'endPoint' and you want o make sure it is provided in the JSON file, then the following code will throw an exception if it is not provided.

@JsonCreator
public QuerySettings(@JsonProperty(value = "endPoint", required = true) String endPoint) {
        this.endPoint = endPoint;
}

I found this link helpful to understand the Jackson annotations. It also well explains why required=true is not enough and counter-intuitive to its name.

Share:
43,848

Related videos on Youtube

Ren
Author by

Ren

Scala Developer and general tech enthusiast.

Updated on September 23, 2021

Comments

  • Ren
    Ren over 2 years

    I'm using Jackson's readValue() method on an object mapper to read from a JSON file and convert it into my java object.

    eg.

    mapperObject.readValue( node, MyTargetClass.class )
    

    Are there any annotations that I can set on MyTargetClass to enforce required attributes? For example, if I have a JSON object with properties ABC,DEF and GHI, and my Json is the following

    {
      "ABC" : "somevalue"
      "DEF" : "someothervalue" 
    }
    

    I want it to fail somehow, and only succeed on the readValue if it contained ABC, DEF and GHI.

  • Gunith D
    Gunith D over 7 years
    Hello. Maybe because it doesn't work yet. See: stackoverflow.com/questions/18320731/…
  • postfuturist
    postfuturist over 7 years
    @GunithDevasurendra It does work in recent versions of Jackson.
  • postfuturist
    postfuturist over 7 years
    I'm using 2.7.3 and it works. However, I found one case where it doesn't: annotating builder methods when using @JsonDeserialize(builder = Foo.Builder.class).
  • abdel
    abdel over 6 years
    i am using spring boot 1.5.9.RELEASE which uses jackson-core 2.8.10 and jackson-annotations 2.8.0 and it doesn't seem to work
  • abdel
    abdel over 6 years
    i checked the source code on 2.8.0 and its javadoc says " Note that as of 2.6, this property is only used for Creator Properties, to ensure existence of property value in JSON: for other properties (ones injected using a setter or mutable field), no validation is performed. Support for those cases may be added in future. State of this property is exposed via introspection, and its value is typically used by Schema generators, such as one for JSON Schema."
  • Eric Taix
    Eric Taix about 6 years
    It works only if you use @JsonProperty in your POJO constructor parameters ! Please note that you have also to set the JsonProperty#value attribute even if this the same as your property's name
  • Javier Aviles
    Javier Aviles almost 5 years
    this is just not correct. @JsonProperty(required = true) ONLY works for serialization, NOT deserialization
  • Klesun
    Klesun about 4 years
    Any examples of how do you use Bean Validation API with readValue() please?
  • StaxMan
    StaxMan about 4 years
    The very first hit googling "jackson validation with bean validation"? baeldung.com/javax-validation -- not within readValue(), first you read (bind), then validate.
  • Klesun
    Klesun about 4 years
    Thanks, I also eventually ended on that link after few hours of searching. My first google hit was this answer.
  • Paul Lam
    Paul Lam over 3 years
    FYI, I find @JsonCreator is not necessary with version 2.9.10.3.
  • Fernando Gabrieli
    Fernando Gabrieli over 2 years
    this worked for me, thank you; it also works on dataclass definitions