Difference between Valid and Validated in Spring
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the Spring Framework, @Valid and @Validated are both essential annotations used for validating input data, typically in the context of web applications using Spring MVC. Despite their apparent similarities, they have distinct purposes and functionalities. Understanding these differences can significantly enhance how validation is handled in your application. This article delves into the nuances of these two annotations, providing technical insights and practical examples.
The Basics of Bean Validation
Before we explore the differences between @Valid and @Validated, it's essential to grasp the underlying concept of Bean Validation.
Bean Validation is a Java specification (JSR 303 and its improved iteration, JSR 380) which provides a standardized way of validating data. Spring Framework supports this specification and integrates it using annotations. These annotations can be applied to Java Bean properties, method parameters, and return values.
Differences Between @Valid and @Validated
1. Origin
@Valid: This annotation is defined in the Java standardjavax.validationpackage. It is a part of the Bean Validation API (JSR 303).@Validated: This annotation is part of Spring and is located in theorg.springframework.validation.annotationpackage. It is specific to Spring's infrastructure for validation.
2. Usage Context
@Valid: Primarily used for method parameter validation. It works straightforwardly with the JSR-303 Bean Validation framework.@Validated: Apart from validating method parameters, it supports validation groups, which allows you to trigger validation rules conditionally based on the context.
3. Validation Group Support
@Valid: Does not support validation groups as it adheres strictly to the standard JSR-303 validation.@Validated: Supports validation groups which is a feature provided by Spring to group different validations together. This is useful in scenarios where you want to apply different validation rules dynamically.
4. Validation Trigger
@Valid: Automatically triggers the validation process when used on method parameters.@Validated: Requires Spring’s infrastructure to trigger validation and is often used in combination with other Spring features, such as method-level validation.
Practical Examples
Example 1: Using @Valid
In the above example, @Valid ensures that the User object provided in the request body complies with any validation constraints (e.g., @NotNull).
Example 2: Using @Validated
Here, @Validated is used on the class, and method parameters are validated based on their constraints when the method placeOrder is invoked.
Example 3: Validation Groups with @Validated
In this example, validation groups are used to apply specific validation rules selectively.
Summary Table
| Aspect | @Valid | @Validated |
| Origin | Java (javax.validation) | Spring (org.springframework.validation.annotation) |
| Validation Group Support | No | Yes |
| Triggered In | Any bean validation context Method parameters in controllers | Spring-managed beans Method parameters in services Class-level validation |
| Primary Use | Basic, standard validation usage | Advanced usages like group validation |
Contextual Considerations
- Error Handling: When using these annotations, it’s advisable to address and handle validation errors appropriately, often by leveraging exception handlers (
@ExceptionHandler) or controller advice (@ControllerAdvice). - Integration with Other Frameworks: Both annotations seamlessly integrate with Spring MVC and other frameworks relying on the Java Bean Validation API, ensuring a consistent validation strategy across different layers of your application.
Conclusion
Understanding the differences between @Valid and @Validated allows developers to effectively and selectively apply validation logic in their applications. Utilizing the right annotation can lead to more robust input validation, ultimately enhancing application stability and user experience. As Spring continues to evolve, these tools remain foundational in building reliable Java applications.

