0

How can I tell jackson to only serialize fields (and not "methods") in a way that I don't need to add @JsonIgnore to all kinds of public methods in all my classes that are irrelevant to serialization? For example in the following class I would expect only the 2 fields: phone, name to be serialized, and not the isEmpty, isValid methods. (which is I think the default in GSon)

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Contact {
    @JsonProperty("p")
    private String phone;
    @JsonProperty("n")
    private String name;

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @JsonIgnore
    public boolean isValid() {
        return !StringHelper.isEmpty(phone);
    }

    @JsonIgnore
    public boolean isEmpty() {
        return StringHelper.isEmpty(phone) && StringHelper.isEmpty(name);
    }
}
Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • Please check if you find it related https://stackoverflow.com/questions/26190851/get-single-field-from-json-using-jackson – www.hybriscx.com Nov 14 '19 at 23:57
  • https://static.javadoc.io/com.fasterxml.jackson.core/jackson-annotations/2.9.2/com/fasterxml/jackson/annotation/JsonAutoDetect.html – JB Nizet Nov 14 '19 at 23:58

0 Answers0