1
1
.
.
1
1
.
.
1
1
V
V
a
a
l
l
i
i
d
d
a
a
t
t
e
e
-
-
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
]
]
[
[
R
R
]
]
This tutorial shows how to Validate Request Parameters when using @RequestParam.
If Parameter is missing or contains invalid value, MissingServletRequestParameterException is thrown.
We will create Endpoint that catches Exception and returns error back to the User.
If Endpoint uses multiple @RequestParam, Exception is thrown only for the first error.
If you want all of the errors to be reported then you need to use DTO approach
DTO - From Request Parameters
DTO - From JSON
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
hello()
MyController
http://localhost:8080/Hello
Browser
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)
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
MyController.java
package com.ivoronline.springboot_validation_requestparameters.controllers;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@Controller
@Validated
public class MyController {
//==================================================================
// HELLO
//==================================================================
@ResponseBody
@RequestMapping("/Hello")
public String hello(
@RequestParam @NotBlank String name,
@RequestParam @NotNull Integer age
) {
return "Hello " + name;
}
//==================================================================
// HANDLE EXCEPTIONS (it only catches first exception)
//==================================================================
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingServletRequestParameterException.class)
public String handleExceptions(MissingServletRequestParameterException exception) {
//GET EXCEPTION DETAILS
String parameterType = exception.getParameterType(); //String
String parameterName = exception.getParameterName(); //name
String message = exception.getMessage(); //Required String parameter 'name' is not present
//RETURN MESSAGE
return message;
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello (reports only first error)
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>