简体   繁体   中英

objectmapper only serialize specific fields

I want to write a function that only serializes a POJO with given implicit field names.

For example,

class Car{
    public int id;
    public String type;
    public Manufacture manufacture;
}

Class Manufacture{
   public int id;
   public String name;
}

if I want to serialize a Car object with a given list(ie [ Car.id , Car.Manufacture.name ]) Then I want to get

{
    Car:{
        id: xxx,
        Manufacture: {
            name: xxx
        }
    }
}

Another example, given list = [ Car.type ]
Then I should get

{
    Car:{
        type: xxx
    }
}

I am currently trying to override the serializeAsField method to check if the field is in the given list, but the problem here is that I don't know the depth, then I cannot correctly compare the current field with the list.

How could I achieve it? Are there any other ways?

使用@JsonIgnore注释标记不需要的字段。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM