What is @Valid Annotation in Spring Boot?
@Valid is an annotation in Spring Boot used to trigger automatic validation of request data, form data, or Java objects before processing them in the application.
It works together with validation annotations like:
@NotNull@NotBlank@Email@Size@Min@Max
In simple words, @Valid tells Spring Boot:
βBefore processing this object, validate all its fields.β
Why is @Valid Used?
Users can send invalid or incomplete data to applications.
Examples:
- Empty username
- Invalid email address
- Short password
- Negative price values
- Blank mobile number
Without validation:
- Invalid data may enter the database
- Business rules may fail
- Application errors may increase
- Security vulnerabilities may occur
@Valid helps prevent these issues by validating incoming data automatically.
Package of @Valid
import jakarta.validation.Valid;
Validation Dependency
To use validation in Spring Boot:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
How @Valid Works Internally
When a request reaches the controller:
- Spring Boot reads request data
- @Valid triggers validation process
- Hibernate Validator checks all validation annotations
- If validation succeeds, controller method executes
- If validation fails, exception is thrown
Simple Example of @Valid
Step 1: Create DTO Class
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
public class UserRequest {
@NotBlank(message = "Name is required")
private String name;
@Email(message = "Invalid email format")
private String email;
@Size(
min = 6,
max = 12,
message = "Password must be between 6 and 12 characters"
)
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Step 2: Use @Valid in Controller
@RestController
@RequestMapping("/users")
public class UserController {
@PostMapping
public String createUser(
@Valid @RequestBody UserRequest request) {
return "User Created Successfully";
}
}
Valid Request Example
{
"name": "Naresh",
"email": "naresh@gmail.com",
"password": "spring123"
}
Output
User Created Successfully
Invalid Request Example
{
"name": "",
"email": "abc",
"password": "12"
}
Validation Error Response
{
"timestamp": "2026-05-18T20:40:00",
"status": 400,
"errors": [
"Name is required",
"Invalid email format",
"Password must be between 6 and 12 characters"
]
}
Most Common Validation Annotations Used with @Valid
| Annotation | Purpose |
|---|---|
| @NotNull | Field cannot be null |
| @NotEmpty | Field cannot be empty |
| @NotBlank | Field cannot contain only spaces |
| Valid email format | |
| @Size | Minimum and maximum length |
| @Min | Minimum numeric value |
| @Max | Maximum numeric value |
| @Positive | Positive number only |
| @Pattern | Regex-based validation |
Using @Valid with Nested Objects
@Valid also supports nested object validation.
Address DTO
public class Address {
@NotBlank(message = "City is required")
private String city;
}
User DTO
public class User {
@NotBlank(message = "Name is required")
private String name;
@Valid
private Address address;
}
Here:
- User object validation triggers Address validation
- Nested validation happens automatically
Using @Valid with Request Parameters
@Valid is mainly used with:
- @RequestBody
- @ModelAttribute
- Nested DTOs
Handling Validation Errors
When validation fails, Spring Boot throws:
MethodArgumentNotValidException
Global Exception Handling for Validation
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(
MethodArgumentNotValidException.class
)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public List<String> handleValidationException(
MethodArgumentNotValidException ex) {
List<String> errors = new ArrayList<>();
ex.getBindingResult()
.getFieldErrors()
.forEach(error ->
errors.add(error.getDefaultMessage()));
return errors;
}
}
Output
[
"Name is required",
"Invalid email format"
]
Real-Time Example in Banking Application
Suppose a banking application allows account registration.
DTO Class
public class AccountRequest {
@NotBlank(message = "Account holder name is required")
private String accountHolderName;
@Positive(message = "Initial balance must be positive")
private double balance;
@Pattern(
regexp = "^[0-9]{10}$",
message = "Mobile number must contain 10 digits"
)
private String mobile;
}
Controller
@PostMapping("/accounts")
public String createAccount(
@Valid @RequestBody AccountRequest request) {
return "Account Created Successfully";
}
Difference Between @Valid and @Validated
| Feature | @Valid | @Validated |
|---|---|---|
| Validation Type | Basic validation | Advanced validation |
| Validation Groups | No | Yes |
| Main Usage | DTO validation | Service-level validation |
Difference Between @NotNull, @NotEmpty, and @NotBlank
| Annotation | Null Allowed | Empty Allowed | Spaces Allowed |
|---|---|---|---|
| @NotNull | No | Yes | Yes |
| @NotEmpty | No | No | Yes |
| @NotBlank | No | No | No |
Advantages of @Valid
- Automatic request validation
- Reduces manual validation code
- Improves application security
- Maintains database consistency
- Provides clean API validation
- Works well with REST APIs and microservices
- Improves user experience
Disadvantages of @Valid
- Improper validation may affect performance
- Complex custom validations require extra coding
- Too many validations may increase DTO complexity
Best Practices for Using @Valid
- Always validate external user input
- Use DTOs instead of entities
- Provide meaningful validation messages
- Handle validation exceptions globally
- Use custom validation for business rules
- Combine client-side and server-side validation
Common Interview Questions on @Valid
What is @Valid in Spring Boot?
@Valid is used to trigger validation on objects before processing them.
What happens if validation fails?
Spring Boot throws MethodArgumentNotValidException.
Can @Valid validate nested objects?
Yes. Nested validation is supported.
What is the difference between @Valid and @Validated?
@Validated supports validation groups, while @Valid is mainly for basic validation.
Why is validation important?
Validation ensures data integrity, application security, and proper business rule enforcement.
Conclusion
@Valid is one of the most important annotations in Spring Boot for validating request data automatically.
It works with Bean Validation and Hibernate Validator to ensure incoming data follows defined rules before processing.
Proper use of @Valid improves application reliability, security, maintainability, and user experience.
Understanding @Valid is essential for Spring Boot developers because validation is widely used in enterprise applications, REST APIs, microservices, and production-grade systems.