简体   繁体   中英

@JsonInclude(JsonInclude.Include.NON_EMPTY) not working when i use a wrapper class and return the result

My Controller Code is

public TestClass<DtoV1> getAllAlliance(@RequestHeader(ID_HEADER) String id,Pageable pageable,                               PagedResourcesAssembler pagedResourcesAssembler)
    {

        Page<DtoV1> pageResource  = serviceV1.findPagedPrefixesById(pageable, id);
        PagedResources<DtoV1> pagedResources = pagedResourcesAssembler.toResource(pageResource);
        TestClass<DtoV1> testClass = new TestClass<>(pageResource.getContent(),pagedResources.getLinks());
        return testClass;
    }

My TestClass Wrapper is

enter code herepublic class TestClass<T> {


public String name;
public Collection<T> content;
public List<Link> links;
public String[] st= new String[0];
public TestClass(Collection<T> content,List<Link> links)
{
    this.content = content;
    this.name="sunil";
    this.links = links;
}
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("_name")
public String getName()
{
    return this.name;
}
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("_st")
public String[] getSt() {
    return st;
}

@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("_embedded")
public Collection<T> getContent()
{
    return this.content;
}

@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("_links")
public List<Link> getLinks() {
    return this.links;
}}

My Result is

{
    "_name": "sunil",
    "_embedded": [
        {
            "key": "0000435",
            "type": "Test",
            "issuing": "temp",
            "issued": "temp",
            "dateIssued": "1989-10-22",
            "links": []
        }],
    "_links": [
        {
            "rel": "first",
            "href": "http://localhost:8080/v1?page=0&size=20",
            "hreflang": null,
            "media": null,
            "title": null,
            "type": null,
            "deprecation": null
        }]
}

If you see i have created an string empty array st in test class it is not coming but why rest are coming

Expected null and empty values will not come as using @JsonInclude(JsonInclude.Include.NON_EMPTY)

The Problem is Thridparty object . So if go with the concept i believe that we can not ignore null or empty of other class objects if they do not contain ignore null annotation

To do this what we can do we can use mixin with objectmapper.

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