Should you use @NotNull and @JsonProperty(required) in the same object
When using @JsonProperty
with required
set to true
on a field or method, Jackson won't perform any validation. See the documentation for further details.
For validation purposes, consider @NotNull
from Bean Validation (a validation provider such as Hibernate Validator is required to perform the validation).
With Swagger, you also can use @ApiModelProperty
and set required
to true
to indicate that a field is mandatory.
Admin
Updated on November 18, 2020Comments
-
Admin over 3 years
So I'm looking at adding constraints to my json views.
I have class similar to this one
public class Person { @JsonProperty(required = true) @NotNull @Size(max = 50) private String name; }
Should I keep both
@JsonProperty(required = true)
and@NotNull
or should I remove one and why?
Just to be clear since Jackson 2.6
@JsonProperty(required = true)
does throw an exception.I'm using springfox-swagger and it looks like when I remove
@JsonProperty(required = true)
the field in the swagger is marked as optional which it isn't.I'm just wondering about the best practice in this situation.