简体   繁体   中英

how to generate dynamic Json in Spring boot

I write a API Restful services. But JSON has to change now. I have to remove some fields in output JSON in output.

my JSON is:

{
    "id": "10001",
    "name": "math",
    "family": "mac",
    "code": "1",
    "subNotes": [
        {
            "id": null,
            "name": "john",
            "family": null,
            "code": "1-1",
            "subNotes": null
        },
        {
            "id": null,
            "name": "cris",
            "family": null,
            "code": "1-2",
            "subNotes": null
        },
        {
            "id": null,
            "name": "eli",
            "family": null,
            "code": "1-3",
            "subNotes": null
        },
    ]
},

But, the requirement is something like this:

{
    "id": "10001",
    "name": "math",
    "family": "mac",
    "code": "1",
    "subNotes": [
        {
            "name": "john",
            "code": "1-1",
        },
        {
            "name": "cris",
            "code": "1-2",
        },
        {
            "name": "eli",
            "code": "1-3",
        },
    ]
},

can I change it without create 2 Object (parent, child)? what's better solution?

You can ignore null fields at the class level by using @JsonInclude(Include.NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null.

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class testDto {

     private String id;
     private String name;
     private String family;
     private String code;
     private List<testDto> subNotes;
}

and then my result:

{
  "id": "10001",
  "name": "math",
  "family": "mac",
  "code": "1",
  "subNotes": [
    {
      "name": "john",
      "code": "1-1"
    },
    {
      "name": "cris",
      "code": "1-2"
    }
  ]
}

Documentation: 3 ways to ignore null fields while converting Java object to JSON using Jackson

I used jackson annotation, it's working.

import com.fasterxml.jackson.annotation.JsonInclude.Include;

public class TestDto implements Serializable {

    @JsonInclude(Include.NON_NULL)
    private String id;

    @JsonInclude(Include.NON_NULL)
    private String name;
    
    @JsonInclude(Include.NON_NULL)
    private String family;
    
    @JsonInclude(Include.NON_NULL)
    private String code;
    
    @JsonInclude(Include.NON_NULL)
    private List<TestDto> subNotes;
    
    //getter and setter ...
    
}

There are 3 way you can refactor the same Object without creating another for your purpose

  1. @JsonIgnore at properties you don't want to be included
  2. @JsonInclude(JsonInclude.Include.NON_NULL) when your field is always null and you do not want that
  3. @JsonInclude(JsonInclude.Include.NON_EMPTY) when you do not want to include empty list

Point 2, 3 is alredy shared in answer so for first option

public class testDto {
 @JsonIgnore
 private String id;
 private String name;
 @JsonIgnore
 private String family;
 private String code;
 @JsonIgnore
 private List<testDto> subNotes;

}

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