What should be my approach to create a DTO for a complex request body in spring boot

I’m new to Spring boot and I need to create a DTO in order to map my request body. Please find the request body below:

 {
   "request":{
      "header":{
         "property_a":"xxx",
         "property_b":"xxx",
         "property_c":123
      },
      "data":{
         "search":{
            "last_name":"xxx",
            "first_name":"xxx",
            "birth_date":"xxx",
            "phone_number":"xxx"
         }
      }
   }

How will I create a DTO to map the above json body request?

Answer

Hopefully below can be one of the approach you can follow –

-----------------------------------com.example.Data.java-----------------------------------

package com.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"search"
})
public class Data {

@JsonProperty("search")
private Search search;

@JsonProperty("search")
public Search getSearch() {
return search;
}

@JsonProperty("search")
public void setSearch(Search search) {
this.search = search;
}

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"request"
})
public class Example {

@JsonProperty("request")
private Request request;

@JsonProperty("request")
public Request getRequest() {
return request;
}

@JsonProperty("request")
public void setRequest(Request request) {
this.request = request;
}

}
-----------------------------------com.example.Header.java-----------------------------------

package com.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"property_a",
"property_b",
"property_c"
})
public class Header {

@JsonProperty("property_a")
private String propertyA;
@JsonProperty("property_b")
private String propertyB;
@JsonProperty("property_c")
private Integer propertyC;

@JsonProperty("property_a")
public String getPropertyA() {
return propertyA;
}

@JsonProperty("property_a")
public void setPropertyA(String propertyA) {
this.propertyA = propertyA;
}

@JsonProperty("property_b")
public String getPropertyB() {
return propertyB;
}

@JsonProperty("property_b")
public void setPropertyB(String propertyB) {
this.propertyB = propertyB;
}

@JsonProperty("property_c")
public Integer getPropertyC() {
return propertyC;
}

@JsonProperty("property_c")
public void setPropertyC(Integer propertyC) {
this.propertyC = propertyC;
}

}
-----------------------------------com.example.Request.java-----------------------------------

package com.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"header",
"data"
})
public class Request {

@JsonProperty("header")
private Header header;
@JsonProperty("data")
private Data data;

@JsonProperty("header")
public Header getHeader() {
return header;
}

@JsonProperty("header")
public void setHeader(Header header) {
this.header = header;
}

@JsonProperty("data")
public Data getData() {
return data;
}

@JsonProperty("data")
public void setData(Data data) {
this.data = data;
}

}
-----------------------------------com.example.Search.java-----------------------------------

package com.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"last_name",
"first_name",
"birth_date",
"phone_number"
})
public class Search {

@JsonProperty("last_name")
private String lastName;
@JsonProperty("first_name")
private String firstName;
@JsonProperty("birth_date")
private String birthDate;
@JsonProperty("phone_number")
private String phoneNumber;

@JsonProperty("last_name")
public String getLastName() {
return lastName;
}

@JsonProperty("last_name")
public void setLastName(String lastName) {
this.lastName = lastName;
}

@JsonProperty("first_name")
public String getFirstName() {
return firstName;
}

@JsonProperty("first_name")
public void setFirstName(String firstName) {
this.firstName = firstName;
}

@JsonProperty("birth_date")
public String getBirthDate() {
return birthDate;
}

@JsonProperty("birth_date")
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}

@JsonProperty("phone_number")
public String getPhoneNumber() {
return phoneNumber;
}

@JsonProperty("phone_number")
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

}

Leave a Reply

Your email address will not be published. Required fields are marked *