@Valid vs @Validated
Reference:Baeldung - Differences in @Valid and @Validated Annotations in Spring
@Valid and @Validated
When applying @Valid, all validation is performed on attributes with constraint conditions set. If you want to keep the constraint conditions declared but only validate specific attributes, you can use @Validated
@Valid vs @Validated
Target
@Validatedincludes the functionality of@Validβ Anywhere
@Validis applied can be replaced with@Validated
@ValidUsed to perform validation on parameters or objects within methods
Not used for class-level validation
@ValidatedSupports both method-level validation and class-level validation
Group Validation Support
@ValidDoes not support group validation
@ValidatedCan specify groups of validation options to review
Can be used to apply or skip validation for specific groups
Validation Method
@Validjavax.validation.Validator
@Validatedorg.springframework.validation.Validator
Integrated with Spring's validation methods
When using the
@Validatedannotation, Spring utilizes Validator implementations to perform validation on the corresponding object
Exception Handling
@Validjavax.validation.ConstraintViolationException is thrown
@ValidatedSpring's MethodArgumentNotValidException or BindException may be thrown, and custom messages or handlers can be configured to handle these exceptions
Last updated