Validating Request Input in Spring Boot
In this tutorial, we will show you how to validate request body and parameters in Spring Boot with examples.
Required Dependencies
Add the following dependencies to your Spring Boot project:
- Spring Boot starter validation - This library helps to validate request inputs.
Add to the pom.xml file:
For Gradle
Add to the build.gradle file:
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: '2.6.3'
You can find the other versions of Spring Boot starter validation in the Spring Boot starter validation Maven repository.
The following is an example that involves validating user's firstName, lastName, email, phoneNumber, age, and address:
import javax.validation.Valid;
import javax.validation.constraints.Email;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class UserRegistrationDto {
@Size(min = 2, max = 40)
@NotEmpty(message = "firstName is required")
private String firstName;
@Size(min = 2, max = 30)
@NotEmpty(message = "lastName is required")
private String lastName;
@Email
@NotEmpty(message = "email is required")
private String email;
@NotEmpty(message = "phoneNumber is required")
private String phoneNumber;
@NotEmpty(message = "age is required")
@Min(18)
@Max(60)
private int age;
@Valid
@NotNull(message = "address")
private AddressDto address;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public AddressDto getAddress() {
return address;
}
public void setAddress(AddressDto address) {
this.address = address;
}
}
The web controller using the above class looks like the following:
import javax.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.request.input.validation.sample.dto.UserRegistrationDto;
@RestController
@RequestMapping(path = "/users")
public class UserController {
@PostMapping(path = "/register")
public void registerNewUser(@Valid @RequestBody UserRegistrationDto userRegistrationDto) {
}
@PutMapping(path = "/{userId}/update")
public void updateUser(@PathVariable(name = "userId", required = true) String userId,
@Valid @RequestBody UserRegistrationDto userRegistrationDto) {
}
@GetMapping
public ResponseEntity<?> getUser(@RequestParam(name = "userId", required = false) String userId,
@RequestParam(name = "email", required = true) String email) {
return ResponseEntity.ok(null);
}
}