@Validated Annotation in Spring: What It Is and How It Works
@Validated annotation in Spring is used to trigger validation on method parameters or beans based on validation rules. It works with Java Bean Validation (JSR-380) to check if input data meets constraints before proceeding.How It Works
The @Validated annotation tells Spring to check the data against validation rules defined on the object or method parameters. Think of it like a security guard who checks if the data fits the rules before letting it pass.
When you add @Validated to a class or method, Spring looks for validation annotations like @NotNull or @Size on the data. If the data breaks any rule, Spring stops the process and reports the errors.
This helps catch mistakes early, like entering a wrong email or missing required fields, making your app more reliable and user-friendly.
Example
This example shows a Spring controller using @Validated to check user input before processing.
import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import jakarta.validation.constraints.Email; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Size; @RestController @Validated public class UserController { public static class User { @NotBlank(message = "Name is required") private String name; @Email(message = "Email should be valid") private String email; @Size(min = 6, message = "Password must be at least 6 characters") private String password; // Getters and setters 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; } } @PostMapping("/register") public String registerUser(@Validated @RequestBody User user) { return "User " + user.getName() + " registered successfully!"; } }
When to Use
Use @Validated when you want Spring to automatically check if input data meets your rules before running your business logic. It is especially useful in web apps where users send data through forms or APIs.
For example, when registering users, submitting orders, or updating profiles, @Validated helps ensure data is correct and complete. This prevents errors later and improves user experience by giving immediate feedback.
Key Points
- @Validated triggers validation on method parameters or beans.
- Works with standard validation annotations like
@NotNull,@Email,@Size. - Helps catch invalid input early and return clear error messages.
- Commonly used in Spring MVC controllers and service layers.
- Requires a validation provider like Hibernate Validator on the classpath.
Key Takeaways
@Validated enables automatic input validation in Spring applications.