Jackson serialization: how to ignore superclass properties

后端 未结 5 943
走了就别回头了
走了就别回头了 2020-12-10 00:58

I want to serialize a POJO class which is not under my control, but want to avoid serializing any of the properties which are coming from the superclass, and not from the fi

5条回答
  •  盖世英雄少女心
    2020-12-10 01:12

    The good use of inheritance is that the child classes extend or add functionality. So the usual way is to serialize the data.

    A workarround would be to use a Value Object (VO) or Data Transfer Object (DTO) with the fields you need to serialize. Steps:

    • Create a VO class with the fields that should be serialized.
    • Use BeanUtils.copyProperties(target VO, source data) to copy the properties
    • Serialize the VO instance.

提交回复
热议问题