Jackson - Required property?
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.
Related videos on Youtube
Comments
-
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 over 7 yearsHello. Maybe because it doesn't work yet. See: stackoverflow.com/questions/18320731/…
-
postfuturist over 7 years@GunithDevasurendra It does work in recent versions of Jackson.
-
postfuturist over 7 yearsI'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 over 6 yearsi 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 over 6 yearsi 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 about 6 yearsIt 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 almost 5 yearsthis is just not correct.
@JsonProperty(required = true)
ONLY works for serialization, NOT deserialization -
Klesun about 4 yearsAny examples of how do you use
Bean Validation API
withreadValue()
please? -
StaxMan about 4 yearsThe very first hit googling "jackson validation with bean validation"? baeldung.com/javax-validation -- not within
readValue()
, first you read (bind), then validate. -
Klesun about 4 yearsThanks, I also eventually ended on that link after few hours of searching. My first google hit was this answer.
-
Paul Lam over 3 yearsFYI, I find @JsonCreator is not necessary with version 2.9.10.3.
-
Fernando Gabrieli over 2 yearsthis worked for me, thank you; it also works on dataclass definitions