Denis Kulagin Denis Kulagin - 1 month ago 8
JSON Question

Jackson: how to serialize only annotated properties

I would like to define my custom serialization strategy (which fields to include), while using Jackson. I know, that I can do it with views/filters, but it introduces very bad thing - using string-representation of field names, which automatically enables problems with auto-refactoring.

How do I force Jackson into serializing only annotated properties and nothing more?

Answer Source

If you disable all auto-detection it should only serialize the properties that you have annotated--whether it be the properties themselves or the getters. Here's a simple example:

private ObjectMapper om;

public void setUp() throws Exception {
    om = new ObjectMapper();
    // disable auto detection
    // if you want to prevent an exception when classes have no annotated properties

public void test() throws Exception {
    BlahClass blahClass = new BlahClass(5, "email", true);
    String s = om.writeValueAsString(blahClass);

public static class BlahClass {
    public Integer id;
    public String email;
    public boolean isThing;

    public BlahClass(Integer id, String email, boolean thing) { = id; = email;
        isThing = thing;