Jackson JSON string into a sequence of objects, ignoring some fields (attributes)

1, plus the properties @JsonIgnore

In this way the role of the global, as long as there is a sequence of this object, it will ignore annotated this part of the field.

2, above, in that way need to add annotations on the bean, acting on the overall situation, but sometimes, we may not need these fields in all cases ignore the subject, the following in this way can support custom filtering.

public final class JsonFilterUtil {

    /**
     * 添加过滤的字段,这里过滤的是 ThinActivityInfo 这个 bean 下的 
     * "startAt", "expiredAt", "extra" 三个字段
     */
    public static void addFilterForMapper(ObjectMapper mapper) {
        SimpleBeanPropertyFilter fieldFilter = SimpleBeanPropertyFilter.serializeAllExcept(
                Sets.newHashSet("startAt", "expiredAt", "extra"));
        SimpleFilterProvider filterProvider = new SimpleFilterProvider().addFilter("fieldFilter", fieldFilter);
        mapper.setFilterProvider(filterProvider).addMixIn(ThinActivityInfo.class, FieldFilterMixIn.class);
    }

    /**
     * 定义一个类或接口
     */
    @JsonFilter("fieldFilter")
    interface FieldFilterMixIn{
    }
}

Original link
http://zhige.me/2019/02/28/2019/02/jackson_serialized_json_ignore_field/

Published 84 original articles · won praise 165 · views 560 000 +

Guess you like

Origin blog.csdn.net/zhige_me/article/details/88044410