Jackson ignore all properties of superclass from external library

前端 未结 2 950
后悔当初
后悔当初 2020-12-20 15:07

I am developing using an ORM where I extend a base orm class to create tables.

For example:

public class Person extends DbItem {
    @JsonIgnore
           


        
相关标签:
2条回答
  • 2020-12-20 15:24

    You can use a Mix-in or @JsonIgnoreProperties

    For the purposes of these examples, the base ORM class and extension are assumed to be:

    public class DbItem {
        public String dbPropertyA;
        public String dbPropertyB;
    }
    

    and

    public class Person extends DbItem {
        public String index;
        public String firstName;
        public String lastName;
    }
    

    respectively.

    Using a Mix-in

    A Mix-in is an abstraction of the de/serialization instructions that Jackson understands from an object itself. It is a way to customize de/serialization of 3rd party classes. In order to define a Mix-in, an abstract class must be created and registered with the ObjectMapper.

    Example Mix-in Definition

    public abstract class PersonMixIn {
        @JsonIgnore public String dbPropertyA;
        @JsonIgnore public String dbPropertyB;
        @JsonIgnore public String index;
    }
    

    Registering the Mix-in

    @Test
    public void serializePersonWithMixIn() throws JsonProcessingException {
        // set up test data including parent properties
        Person person = makeFakePerson();
    
        // register the mix in
        ObjectMapper om = new ObjectMapper()
                .addMixIn(Person.class, PersonMixIn.class);
    
        // translate object to JSON string using Jackson
        String json = om.writeValueAsString(person);
    
        assertFalse(json.contains("dbPropertyA"));
        assertFalse(json.contains("dbPropertyB"));
        assertFalse(json.contains("index"));
        System.out.println(json);
    }
    

    @JsonIgnoreProperties

    If you want to avoid creating a class and configuring the ObjectMapper, the @JsonIgnoreProperties annotation can be utilized. Simply annotate the class you are serializing and list the properties to exclude.

    Example Serializable Object

    @JsonIgnoreProperties({"index", "dbPropertyA", "dbPropertyB"})
    public class Person extends DbItem {
        public String index;
        public String firstName;
        public String lastName;
    }
    

    See It In Action

    @Test
    public void serializePersonWithIgnorePropertiesAnnotation() throws JsonProcessingException {
        // set up test data including parent properties
        Person person = makeFakePerson();
    
        ObjectMapper om = new ObjectMapper();
    
        // translate object to JSON string using Jackson
        String json = om.writeValueAsString(person);
    
        assertFalse(json.contains("dbPropertyA"));
        assertFalse(json.contains("dbPropertyB"));
        assertFalse(json.contains("index"));
        System.out.println(json);
    }
    
    0 讨论(0)
  • 2020-12-20 15:25

    You want to do custom field level serialization. This will be a bit more work to maintain your code base, but is by far the simplest solution. See Jackson JSON custom serialization for certain fields for implementation details.

    0 讨论(0)
提交回复
热议问题