简体   繁体   English

杰克逊:忽略空对象

[英]Jackson: ignore empty object

If the value of the field name in the inner class is null, then如果inner类中字段name的值为null,则

{"id": 123, "inner": {}} {“id”:123,“内部”:{}}

is returned as a response.作为响应返回。 However, if the value to inner is returned as {}, the key-value pair但是,如果inner的值作为 {} 返回,则键值对

"inner": {} “内部”:{}

should be ignored entirely, so the response is应该完全忽略,所以响应是

{"id": 123} {“身份证”:123}

. . How can this be achieved?如何做到这一点? I thought it could be achieved with @JsonInclude(JsonInclude.Include.NON_NULL) but it did not solve the problem.我认为可以使用@JsonInclude(JsonInclude.Include.NON_NULL)来实现,但它并没有解决问题。

@RestController
public class ItemController {
    @Autowired
    ItemService itemService;

    @GetMapping("/item")
    public Item getItem() {
        return itemService.getItem(); // {"id": 123,"inner": {}} is returned but should be {"id": 123}
    }
}
@Service
public class ItemService {
    public Item getItem() {
        Item.Inner inner = new Item.Inner();
        Item item = new Item();
        item.setInner(inner);
        item.setId(123);

        return item;
    }
}
@Data
public class Item {
    private int id;
    private Inner inner;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @Data
    public static class Inner {
        private String name;
    }
}

You can use @JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = Inner .class) on Inner field in Item class, but to make this work you need to override hashcode and equals method in Item class.您可以在Item类的Inner字段上使用@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = Inner .class) ,但要完成这项工作,您需要覆盖Item类中的 hashcode 和 equals 方法。

Basically, the equals method in Inner class will be called when deciding whether or not inner object should be excluded or not.基本上,在决定是否应排除内部对象时,将调用 Inner 类中的equals方法。 If equals returns true , then the object will be excluded from response.如果 equals 返回true ,则该对象将从响应中排除。 So make the method match anything you want to filter out and return false for anything that should be written out normally.因此,使该方法匹配您想要过滤掉的任何内容,并为应该正常写出的任何内容返回false

You can read more details here您可以在此处阅读更多详细信息

For your particular case, the following code should work :对于您的特定情况,以下代码应该可以工作:

@Data
public class Item {
    private int id;

    @JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = Inner .class)
    private Inner inner;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Inner getInner() {
        return inner;
    }

    public void setInner(Inner inner) {
        this.inner = inner;
    }

    
    @Data
    public static class Inner {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Inner inner = (Inner) o;
            return Objects.equals(name, inner.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(name);
        }
    }
}

NOTE : @JsonInclude(JsonInclude.Include.NON_NULL) will ignore null fields, so if you put it on class level for Inner class you will get response like this (it will ignore name field in Inner class since it's value is null ) :注意@JsonInclude(JsonInclude.Include.NON_NULL)将忽略空字段,因此如果将其放在Inner类的类级别上,您将得到这样的响应(它将忽略 Inner 类中的name字段,因为它的值为null ):

{
    "id": 123,
    "inner": {}
}

That will also not work if you put it on class level for class Item since your Inner object is not null, it's empty object, so that will produce this:如果您将它放在类Item的类级别上,这也将不起作用,因为您的Inner对象不为空,它是空对象,因此将产生以下结果:

{
    "id": 123,
    "inner": {
        "name": null
    }
}

Please try with below at the class level.请在班级级别尝试以下内容。

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)

Thank you谢谢

By using JsonInclude annotation, it will remove the empty values.通过使用JsonInclude注释,它将删除空值。

@Data
@JsonInclude(JsonInclude.Include.NOT_NULL)
public class Item {
    private int id;
    private Inner inner;

    public static class Inner {
        private String name;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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