General process looks like this
● Frontend stores data into DTO (JavaScript Object)
● Frontend Serializes DTO into JSON
● Frontend sends JSON (Postman with JSON Body)
● Controller receives JSON
● Controller Deserializes JSON into DTO (@RequestBody PersonDTO personDTO)
● Controller works with DTO (Java Object)
Serialization is process of turning an Object into a Structured Data Format like: JSON, XML.
Serialization can be done to store or send Object.
Deserialization is reverse process of turning Structured Data Format back into an Object.
Schema
MyController.java
@Controller
public class MyController {
@ResponseBody
@RequestMapping("/AddPerson")
public PersonDTO addPerson(@RequestBody PersonDTO personDTO) {
//Controller works with Deserialized personDTO Object
return PersonDTO; //Controller returns Serialized PersonDTO as JSON
}
}
Spring Boot uses Jackson Library to Serialize/Deserialize JSON/Object.
Jackson Library uses reflection to access private Properties/Setters/Getters.
To Deserialize JSON into Object Jackson will use
● Constructor (if present)
● Setters (if Constructor is not present)
● Properties (if neither Setters not Constructor are present)
Following Annotations can be used above Properties/Setters/Getters or for Constructor Parameters
● @JsonProperty specifies which JSON Property to map with Annotated Property/Setter/Getter/Constructor Parameter
● @JsonFormat specifies in which format is Value of JSON Property
(so that it could be properly converted into Value that can be stored into Object Property)