Validated, Valid, Validator, You Know the Difference Between Them
1. Conclusion comes first
Valid VS Validated Similarities
•Both methods and parameters can be validated
•@Valid and @Validated Both annotations cause standard bean validation to be applied.
If the validation fails, a BindException will be thrown and a 400 (BAD_REQUEST) response will be thrown; or validation errors can be handled locally in the controller through the Errors or BindingResult parameters . In addition, if there is an @RequestBody annotation before the parameter , the validation error will throw a MethodArgumentNotValidException exception .
JSR 380 is the Java API specification for bean validation and is part of Jakarta EE and JavaSE . This ensures that the bean's properties meet certain conditions, using annotations such as @NotNull , @Min , and @Max .
This release requires Java 8 or later and takes advantage of new features added in Java 8, such as type annotations and support for new types like Optional and LocalDate .
For complete information on the specification, continue reading JSR 380 .
What is the difference between Valid VS Validated?
oIs the JSR-303 specification standard annotation support, is a markup annotation.
oAnnotation supports ElementType#METHOD , ElementType#FIELD , ElementType#CONSTRUCTOR ,
oElementType#PARAMETER , ElementType#TYPE_USE
oIt is a custom annotation made by Spring that enhances the grouping function.
oAnnotation supports ElementType#TYPE , ElementType#METHOD , ElementType#PARAMETER
Difference between @Valid and @Validated
Validated, Valid, Validator.Validator
Bean Validation 2.0 (JSR 380) defines a metadata model and API for entity and method validation , and Hibernate Validator is the best implementation currently
Validator interface has three methods that can be used to validate the entire entity or just a single property of the entity
•Validator#validate () validates all constraints on all beans
•Validator#validateProperty () validates a single property
•Validator#validateValue () checks if a single property of a given class can be successfully validated
Whether it is requestBody parameter verification or method-level verification , Hibernate Validator is ultimately called to perform verification, and Spring Validation just does a layer of encapsulation .
Validating user input is a common feature in most of our applications. In the Java ecosystem, we use the Java Standard Bean Validation API exclusively to support this. Also, as of version 4.0, this is also well integrated with Spring.
In the next sections, let's understand them in detail.
@Valid and @Validated annotations
In Spring, we use JSR-303's @Valid annotation for method level validation . Also, we use it to mark member properties for validation . However, this annotation does not support group validation .
Groups help limit constraints applied during validation. A special use case is UI wizards . Here, in the first step, we may have some field subgroup. In subsequent steps, there may be another group belonging to the same bean. So we need to apply constraints on these limited fields at every step, but @Valid doesn't support this.
In this case, for the group level, we have to use Spring's @Validated, which is a variant of JSR-303's @Valid . This is used at the method level. For markup member properties, we continue to use the @Valid annotation.
Now, let's jump right in and see the usage of these annotations with an example.
Let's consider a simple user registration developed with Spring Boot . First, we will only have the name and password properties :
After confirming that the test ran successfully, we now extend the functionality. The next logical step is to convert it to complex user registration. For the first step, the name and password remain the same. In the second step, we will get additional information like age and phone . So we'll update our domain object with these additional fields :
However, this time we will notice that the previous test failed. This is because we didn't pass in the age and phone fields . To support this behavior, we need group validation and the @Validated annotation.
To do this, we need to group the fields, creating two different groups. First, we need to create two marker interfaces. One for each group or each step individually. We can refer to our article on group validation to see how it is implemented. Here, let's focus on the differences in annotations.
We will have the BasicInfo interface for the first step and AdvanceInfo for the second step . Additionally, we will update the UserAccount class to use these marker interfaces as follows:
Next, let's see how @Valid triggers validation of nested properties.
Validated, Valid, Validator.Use @Valid Nested Validation
The @Valid annotation is used to validate nested properties . This triggers validation of nested objects. For example, in our current scenario, let's create a UserAddress Object:
5. Combining @Valid and @Validated perform set check
If the request body directly passes a json array to the backend, and you want to perform parameter verification on each item in the array. At this point, if we directly use the list or set under java.util.Collection to receive data, the parameter verification will not take effect! We can use a custom list collection to receive parameters:
•Wrap the List type and declare the @Valid annotation
import javax.validation.Valid ; _
import java.util .*;
Validated, Valid, Validatorin conclusion
Validated, Valid, Validator.In conclusion, for any basic validation, we will use the JSR @Valid annotation on the method call. On the other hand, for any group validation, including group sequences , we need to use Spring's @Validated annotation in our method calls . The @Valid annotation is also required to trigger validation of nested properties.
Validated is still AOP . In the method verification, the AOP dynamic interception method is used, and the JSR303 Validator is used to complete the verification. In the attribute verification of Bean, it is based on the life cycle of Bean, and the verification is completed before and after its initialization.