I have the following response structure.
{
"URL": "",
"sites": [
{
"details": "",
"details2": "",
"details3": [
{
"moreDetails": "",
"moreDetails2": "",
}
]
},
{
"details": "",
"details2": "",
"details3": [
{
"moreDetails": "",
"moreDetails2": "",
}
]
}
]
}
This is accomplished by having multiple models.
SitesResponse
Sites
Details
MoreDetails
All of them have getter and setters and I just set up the response in the controller. Is there a way to move ALL of the response to a parent object without rewriting the hierarchy?
For an example, something like this:
{
"results": {
"URL": "",
"sites": [
{
"details": "",
"details2": "",
"details3": [
{
"moreDetails": "",
"moreDetails2": ""
}
]
},
{
"details": "",
"details2": "",
"details3": [
{
"moreDetails": "",
"moreDetails2": ""
}
]
}
]
}
}
you have to put them inside another object as below :
Result Model :
@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;
}
}
Site Model:
@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;
}
Details Model :
@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;
}
}
Holder Model:
@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;
}
}
then you can pass the Holder
Model as a response