java—将responseentity Package 到另一个对象中

jgzswidk  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(260)

我有以下的React结构。

{
    "URL": "",
    "sites": [
        {
            "details": "",
            "details2": "",
            "details3": [
                {
                    "moreDetails": "",
                    "moreDetails2": "",
                }
            ]
        },
        {
            "details": "",
            "details2": "",
            "details3": [
                {
                    "moreDetails": "",
                    "moreDetails2": "",
                }
            ]
        }
    ]
}

这是通过拥有多个模型来实现的。

SitesResponse
Sites
Details
MoreDetails

它们都有getter和setter,我只是在控制器中设置响应。有没有一种方法可以将所有响应移到父对象而不重写层次结构?
例如,如下所示:

{
   "results": {
      "URL": "",
      "sites": [
         {
            "details": "",
            "details2": "",
            "details3": [
               {
                  "moreDetails": "",
                  "moreDetails2": ""
               }
            ]
         },
         {
            "details": "",
            "details2": "",
            "details3": [
               {
                  "moreDetails": "",
                  "moreDetails2": ""
               }
            ]
         }
      ]
   }
}
zphenhs4

zphenhs41#

必须将它们放入另一个对象中,如下所示:
结果模型:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "URL",
    "sites"
})
public class Results {

    @JsonProperty("URL")
    private String uRL;
    @JsonProperty("sites")
    private List < Site > sites = null;

    @JsonProperty("URL")
    public String getURL() {
        return uRL;
    }

    @JsonProperty("URL")
    public void setURL(String uRL) {
        this.uRL = uRL;
    }

    @JsonProperty("sites")
    public List < Site > getSites() {
        return sites;
    }

    @JsonProperty("sites")
    public void setSites(List < Site > sites) {
        this.sites = sites;
    }

}

站点模型:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "details",
    "details2",
    "details3"
})
public class Site {

    @JsonProperty("details")
    private String details;
    @JsonProperty("details2")
    private String details2;
    @JsonProperty("details3")
    private List < Details3 > details3 = null;

    @JsonProperty("details")
    public String getDetails() {
        return details;
    }

    @JsonProperty("details")
    public void setDetails(String details) {
        this.details = details;
    }

    @JsonProperty("details2")
    public String getDetails2() {
        return details2;
    }

    @JsonProperty("details2")
    public void setDetails2(String details2) {
        this.details2 = details2;
    }

    @JsonProperty("details3")
    public List < Details3 > getDetails3() {
        return details3;
    }

    @JsonProperty("details3")
    public void setDetails3(List < Details3 > details3) {
        this.details3 = details3;
    }

详细型号:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "moreDetails",
    "moreDetails2"
})
public class Details3 {

    @JsonProperty("moreDetails")
    private String moreDetails;
    @JsonProperty("moreDetails2")
    private String moreDetails2;

    @JsonProperty("moreDetails")
    public String getMoreDetails() {
        return moreDetails;
    }

    @JsonProperty("moreDetails")
    public void setMoreDetails(String moreDetails) {
        this.moreDetails = moreDetails;
    }

    @JsonProperty("moreDetails2")
    public String getMoreDetails2() {
        return moreDetails2;
    }

    @JsonProperty("moreDetails2")
    public void setMoreDetails2(String moreDetails2) {
        this.moreDetails2 = moreDetails2;
    }

}

支架型号:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "results"
})
public class Holder {

    @JsonProperty("results")
    private Results results;

    @JsonProperty("results")
    public Results getResults() {
        return results;
    }

    @JsonProperty("results")
    public void setResults(Results results) {
        this.results = results;
    }

}

然后你就可以通过考试了 Holder 作为回应的模型

相关问题