最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spring boot - JsonInclude.Include.NON_EMPTY Doesn't Work after I implemented custom serializer on an attribute - Stack O

programmeradmin9浏览0评论

I have a class which has Include.NON_EMPTY configuration on class level.

@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
public class Item extends ExtraInfo {

   String id;

   List<String> oldAttribute;

}

@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class ExtraInfo {

   @JsonSerialize(using = NewAttributeSerializer.class)
   List<BigInteger> newAttribute;
}

I recently implemented a new Serializer NewAttributeSerializer which looks like this:

public class NewAttributeSerializer extends JsonSerializer<List<AnObject>> {

    @Override
    public void serialize(List<AnObject> objects, JsonGenerator gen, SerializerProvider provider) throws IOException {
        List<BigInteger> objIds =  objects.stream.filter(AnObject::isEligible).map(AnObject::geId).collect(Collectors.toList())
        provider.defaultSerializeValue(objIds, gen);
    }

    @Override
    public boolean isEmpty(SerializerProvider provider, List<AnObject> value) {
        return value.isEmpty();
    }

Before I implemented this new serializer, I didn't see oldAttribute in response if it had no entries. But now, after implementing this new Serializer, I can see oldAttribute=[] is coming in response if it has empty list. (It should ideally be ignored from final json response because of Include.NON_EMPTY on Item class level.)

So my question is, is there any connection between child class serializer and parent class serializer config?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论