2
2
.
.
1
1
1
1
.
.
1
1
F
F
r
r
o
o
m
m
R
R
e
e
q
q
u
u
e
e
s
s
t
t
P
P
a
a
r
r
a
a
m
m
e
e
t
t
e
e
r
r
s
s
-
-
U
U
s
s
i
i
n
n
g
g
S
S
e
e
t
t
t
t
e
e
r
r
s
s
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial shows how to Deserialize HTTP Request Parameters into DTO. (how to automatically load them into DTO)
Unfortunately there is no way to map HTTP Request Parameters to DTO Parameters if they have different names.
To do so you can use workaround described in From Request Parameters - Using Map where
HTTP Request Parameters are first loaded into Map<String, Object> and then
ObjectMapper loads the Map into PersonDTO
Syntax
@RequestMapping("/Hello")
public String hello(PersonDTO personDTO) { ... }
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
PersonDTO
http://localhost:8080/Hello
Tomcat
Browser
MyController
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_deserialize_requestparameters (add Spring Boot Starters from the table)
Create Package: DTO (inside main package)
Create Class: PersonDTO.java (inside package controllers)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside package controllers)
PersonDTO.java
package com.ivoronline.springboot_deserialize_requestparameters.DTO;
public class PersonDTO {
//PROPERTIES
public String name;
public Integer age;
//SETTERS (used for deserialization)
public void setName(String name) { this.name = name; }
public void setAge (Integer age ) { this.age = age; }
}
MyController.java
package com.ivoronline.springboot_deserialize_requestparameters.controllers;
import com.ivoronline.springboot_deserialize_requestparameters.DTO.PersonDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@ResponseBody
@RequestMapping("/Hello")
public String hello(PersonDTO personDTO) {
return "Hello " + personDTO.name;
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello?name=John&age=20
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>