2
2
.
.
1
1
1
1
D
D
T
T
O
O
-
-
D
D
e
e
s
s
e
e
r
r
i
i
a
a
l
l
i
i
z
z
e
e
I
I
n
n
f
f
o
o
Following tutorials show how to work with Data Transfer Objects (DTO) which are used to transfer data between
Applications (Frontend and Backend)
Layers of the same Application (Controller and Service Layer)
In certain situations instead of DTO you can reuse Entity to transfer data.
But if data from multiple Entities needs to be transferred then DTO is used.
For each pair of HTTP Request and Response you can have two DTOs
RequestDTO to Deserialize data received from the Frontend
ResponseDTO to Serialize data returned to the Frontend
Content
DTO usage between Frontend and Backend
DTO usage between Application Layers
Jackson Library
Model Mapper
Related Previous Tutorials
Data Transfer Object (DTO) Explains theory behind using DTO between Controller and Service Layer
Annotation - (Spring) @RequestBody Explains how to convert HTTP Request Parameters into DTO
D
D
T
T
O
O
u
u
s
s
a
a
g
g
e
e
b
b
e
e
t
t
w
w
e
e
e
e
n
n
F
F
r
r
o
o
n
n
t
t
e
e
n
n
d
d
a
a
n
n
d
d
B
B
a
a
c
c
k
k
e
e
n
n
d
d
[
[
R
R
]
]
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
}
}
J
J
a
a
c
c
k
k
s
s
o
o
n
n
L
L
i
i
b
b
r
r
a
a
r
r
y
y
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)
Serialize
Deserialize
public class Person {
String name;
Integer age;
}
{
"name" : "John",
"age" : "20"
}
DTO
Serialize
Deserialize
DTO
Frontend
Backend
Network
var Person = {
"name" : "John",
"age" : "20"
}
JavaScript Object
Java Object
JSON
D
D
T
T
O
O
u
u
s
s
a
a
g
g
e
e
b
b
e
e
t
t
w
w
e
e
e
e
n
n
A
A
p
p
p
p
l
l
i
i
c
c
a
a
t
t
i
i
o
o
n
n
L
L
a
a
y
y
e
e
r
r
s
s
General process looks like this
Controller sends DTO to Service Layer (after it Deserializing DTO from JSON received by Frontend)
Service Layer converts DTO into Entities (using Object Mappers)
Service Layer uses Entities to perform Business Logic
Service Layer stores resulting Data into DTO
Service Layer return DTO to Controller (which Serializes DTO into JSON and returns it to Frontend)
Object Mappers are used to convert one Object into another - in our case to convert DTO into Entities.
There are different Libraries that implement Object Mapping in different ways.
Controller sends DTO to Service Layer Service Layer converts DTO into Entities
AuthorBookDTO is mapped into Author and Book Entities
AuthorBookDTO
public class Author {
public Integer id;
public String name;
public Integer age;
}
public class Book {
public Integer id;
public String title;
}
public class AuthorBookDTO {
public String name;
public Integer age;
public String title;
}
{
"name" : "John",
"age" : 20,
"title": "Book about dogs"
}
Controller
Service Layer
DTO
M
M
o
o
d
d
e
e
l
l
M
M
a
a
p
p
p
p
e
e
r
r
[
[
R
R
]
]
Often there is a need to convert DTO into Entity and vice versa which is done using Model Mapper Class that
maps DTO into Entity and vice versa
by default matches Properties with the same name
transfers matching Properties between DTO and Entity by using their setters and getters
(both DTO and Entity must have setters and getters since Properties aren't referenced directly even if they are public)
There is no Spring Boot Starter for Model Mapper so Maven Dependency has to be manually added to pom.xml.
pom.xml [R]
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.9</version>
</dependency>