1
1
.
.
1
1
.
.
2
2
V
V
a
a
l
l
i
i
d
d
a
a
t
t
e
e
-
-
D
D
T
T
O
O
-
-
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
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial shows how to perform Validation while Deserializing HTTP Request Parameters into DTO.
If Parameter is missing or contains invalid value, BindException is thrown.
You can create Endpoint that catches BindException and returns all validation errors back to the User.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_dto_json_object_constructor (add Spring Boot Starters from the table)
Edit File: pom.xml (add validation dependency)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside package controllers)
Create Package: DTO (inside main package)
Create Class: PersonDTO.java (inside package controllers)
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation </artifactId>
</dependency>
PersonDTO.java
package com.ivoronline.springboot_deserialize_requestparameters.DTO;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
public class PersonDTO {
//PROPERTIES
@NotBlank(message = "Name is mandatory")
public String name;
@NotNull
public Integer age;
//SETTERS (used for deserialization)
public void setName(String name) { this.name = name; }
public void setAge (Integer age ) { this.age = age; }
}
PersonDTO
http://localhost:8080/Hello
Tomcat
Browser
MyController
MyController.java
package com.ivoronline.springboot_deserialize_requestparameters.controllers;
import com.ivoronline.springboot_deserialize_requestparameters.DTO.PersonDTO;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class MyController {
//==================================================================
// HELLO
//==================================================================
@ResponseBody
@RequestMapping("/Hello")
public String hello(@Valid PersonDTO personDTO) {
return "Hello " + personDTO.name;
}
//==================================================================
// HANDLE EXCEPTIONS
//==================================================================
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(BindException.class)
public Map<String, String> handleExceptions(BindException exception) {
//CREATE MAP FOR STORING ERRORS
Map<String, String> errors = new HashMap<>();
//ITERATE THROUGH ERRORS
List<ObjectError> objectErrors = exception.getAllErrors();
for(ObjectError objectError : objectErrors) {
//GET ERROR
FieldError fieldError = (FieldError) objectError;
String fieldName = fieldError.getField();
String errorMessage = fieldError.getDefaultMessage();
//STORE ERROR
errors.put(fieldName, errorMessage);
}
//RETURN ERRORS
return errors;
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>