I want to ignore a field when a it can be retrieved:
public boolean isActiveProduct() {
....
...
if (product != null) {
return product.isActiveProduct();
}
// ignore this field and don't return it in reponse
}
Any help will be appreciated!
CodePudding user response:
Create another method (possibly less visible) for JSON serialization, which returns null
, if the product is null
. Apply these annotations to this new method:
@JsonProperty("activeProduct")
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@Nullable // optional annotation for static code analyser
protected Boolean isActiveProductOrNull() {
if (product != null) {
return product.isActiveProduct();
} else {
return null;
}
}
Tag the current method as @JsonIgnore
.
@JsonIgnore
public boolean isActiveProduct() {
if (product != null) {
return product.isActiveProduct();
} else {
return false; // or throw an exception, if you want.
}
}