简体   繁体   中英

How to add additional JSON properties like this in API in Spring Boot application?

I am implementing a REST API which send and receive data with JSON (I am totally new to this API design). I am using Spring framework. I want to show the status-code and iso outside the main JSON body in my API. What should I do??

Like this:

[
  [
{
   "status":"200",
    "iso":"03"
  }
],  
    {
        "id": 0,
        "lat": "33.93911",
        "long": "67.709953",
        "provinceState": ""
    },
    {
        "id": 1,
        "lat": "41.1533",
        "long": "20.1683",
        "provinceState": ""
    }

]

Here are my pojo classes.

Entity



@Entity
public class GlobalDeadEntity {

    @Id
    private Long id;
    private int status;
    private int iso;


    public GlobalDeadEntity() {
    }

    public GlobalDeadEntity(Long id, Integer totalReportedDead, Integer totalDeadToday) {
        this.id = id;
        this.status = totalReportedDead;
        this.iso = totalDeadToday;
    }

    @JsonIgnore
    @JsonProperty(value = "id")
    public Long getId() {
        return id;
    }

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

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getIso() {
        return iso;
    }

    public void setIso(int iso) {
        this.iso = iso;
    }
}

Thanks for your help.

i didn't notice location POJO in your question also to follow the best practice you have to separate the DB model and web model, anyway, you have to include them in one POJO:

@Entity
public class GlobalDeadEntity {

    @Id
    private Long id;
    private int status;
    private int iso;


    public GlobalDeadEntity() {
    }

    public GlobalDeadEntity(Long id, Integer totalReportedDead, Integer totalDeadToday) {
        this.id = id;
        this.status = totalReportedDead;
        this.iso = totalDeadToday;
    }

    @JsonIgnore
    @JsonProperty(value = "id")
    public Long getId() {
        return id;
    }

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

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getIso() {
        return iso;
    }

    public void setIso(int iso) {
        this.iso = iso;
    }
}

suppose the below Info are representing the location info: :

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "id",
        "lat",
        "long",
        "provinceState"
})
public class Info {

    @JsonProperty("id")
    private Integer id;
    @JsonProperty("lat")
    private String lat;
    @JsonProperty("long")
    private String _long;
    @JsonProperty("provinceState")
    private String provinceState;

    public Info(Integer id, String lat, String _long, String provinceState) {
        this.id = id;
        this.lat = lat;
        this._long = _long;
        this.provinceState = provinceState;
    }

    @JsonProperty("id")
    public Integer getId() {
        return id;
    }

    @JsonProperty("id")
    public void setId(Integer id) {
        this.id = id;
    }

    @JsonProperty("lat")
    public String getLat() {
        return lat;
    }

    @JsonProperty("lat")
    public void setLat(String lat) {
        this.lat = lat;
    }

    @JsonProperty("long")
    public String getLong() {
        return _long;
    }

    @JsonProperty("long")
    public void setLong(String _long) {
        this._long = _long;
    }

    @JsonProperty("provinceState")
    public String getProvinceState() {
        return provinceState;
    }

    @JsonProperty("provinceState")
    public void setProvinceState(String provinceState) {
        this.provinceState = provinceState;
    }

}

then you can pass GlobalDeadEntityHolder instead of passing GlobalDeadEntity directly as below:

public class GlobalDeadEntityHolder {

    private List<Info> InfoList;
    private GlobalDeadEntity GlobalDeadEntity;

    public GlobalDeadEntityHolder(List<Info> infoList, GlobalDeadEntity globalDeadEntity) {
        InfoList = infoList;
        GlobalDeadEntity = globalDeadEntity;
    }

    public List<Info> getInfoList() {
        return InfoList;
    }

    public void setInfoList(List<Info> infoList) {
        InfoList = infoList;
    }

    public GlobalDeadEntity getGlobalDeadEntity() {
        return GlobalDeadEntity;
    }

    public void setGlobalDeadEntity(GlobalDeadEntity globalDeadEntity) {
        GlobalDeadEntity = globalDeadEntity;
    }
} 

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