I have a class with few fields
static final class Sample {
Enum A, B, C, D;
Sample(A, B, C) {
// A,B,C init; not D (it's null)
}
}
When I create an instance of Sample using 3 parameter constructor, I want to ignore 4th one while sending as response to an API call.
How can I achieve this? I can't use JsonIgnore because in other flow, D will have some non null values;
您可以通过使用 @JsonInclude(Include.NON_NULL) 在类级别忽略空字段以仅包含非空字段,从而排除任何值为空的属性。
You have to add @JsonInclude(JsonInclude.Include.NON_NULL) of (com.fasterxml.jackson.annotation.JsonInclude;) on class level as below:
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
class Sample {
Enum A, B, C, D;
Sample(A, B, C) {
// A,B,C init; not D (it's null)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.