Clean • Professional
Validation is a critical part of building reliable and secure REST APIs. It ensures that the data sent by the client is correct, complete, and safe before the application processes or stores it.
Without proper validation, invalid or malformed input can cause runtime errors, corrupt the database, or expose security vulnerabilities.
Validation is the process of verifying that incoming client data meets predefined rules before it is processed by the server.
Validation helps to:
Simple Examples:
Spring Boot provides built-in validation support using Bean Validation (JSR 380), typically implemented via Hibernate Validator.
Validation Flow
ClientJSON
↓
@RequestBody
↓
@Valid
↓
BeanValidationAnnotations
↓
✔ControllerLogic
❌400BadRequest
How it works internally:
@Valid triggers validation in the controllerif-else checks are required| Annotation | Description |
|---|---|
@NotNull | Field must not be null |
@NotBlank | String must not be null, empty, or whitespace |
@Size(min, max) | Length constraints |
@Email | Valid email format |
@Min(value) | Minimum numeric value |
@Max(value) | Maximum numeric value |
@Positive | Must be a positive number |
@Pattern(regex) | Must match a regex |
@Past / @Future | Date validation |
We are creating a User Registration API.
The client sends user details, and the server must validate the input before saving.
User DTO with Validation Rules
publicclassUserDTO {
@NotBlank(message = "Name is required")
private String name;
@Email(message = "Invalid email address")
private String email;
@Min(value = 18, message = "Age must be at least 18")
privateint age;
// getters and setters
}
What happens here?
@NotBlank → Name cannot be empty@Email → Email format must be valid@Min(18) → Age must be 18 or aboveController Using @Valid
@RestController
@RequestMapping("/users")
publicclassUserController {
@PostMapping("/register")
public ResponseEntity<String>registerUser(
@Valid@RequestBody UserDTO userDTO) {
// Executes only if validation passes
return ResponseEntity
.status(201)
.body("User registered successfully");
}
}
What happens here?
@RequestBody → Converts JSON into Java object@Valid → Applies validation rules{
"name":"Alice",
"email":"[email protected]",
"age":25
}
Response
HTTP/1.1201 Created
User registered successfully
The request data satisfies all validation rules, so the controller method executes successfully.
{
"name":"",
"email":"alice.com",
"age":10
}
Automatic Error Response (400 Bad Request)
{
"timestamp":"2026-01-01T12:00:00",
"status":400,
"errors":[
"Name is required",
"Invalid email address",
"Age must be at least 18"
]
}
What Spring Boot Does Automatically
@ValidFor better control and standardized error responses, you can use:
@ControllerAdvice@ExceptionHandler(MethodArgumentNotValidException.class)This improves API usability and developer experience, especially in large applications.
@Valid with @RequestBodyResponseEntity for proper HTTP status codesValidation in Spring Boot REST APIs ensures that your application receives clean, safe, and predictable data.
By using Bean Validation annotations with @Valid, Spring Boot automatically handles input validation and error responses, making your APIs robust, secure, and production-ready.