How to make Jackson ignore a get() method when serializing an object

If you mark the getter method with the @JsonIgnore annotation it should not be serialized. Here is an example:

public class JacksonIgnore {

    public static class Release {
        public final String version;

        public Release(String version) {
            this.version = version;
        }
    }
    public static class Project {
        public final String name;

        public Project(String name) {
            this.name = name;
        }

        @JsonIgnore
        public List<Release> getAllReleases() {
            return Arrays.asList(new Release("r1"), new Release("r2"));
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new Project("test")));
    }
}

Output:

{
  "name" : "test"
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)