Difference between @validated and @valid annotations in springboot

In Spring Boot, both @Valid and @Validated annotations are used to trigger validation on data (e.g., request body, method parameters), but they have different use cases and capabilities.

1. @Valid Annotation

  • Source: It comes from the Java Bean Validation specification (javax.validation.Valid or jakarta.validation.Valid depending on the version).
  • Usage: Primarily used for simple, default validation without the need for groups.
  • Functionality:
    • Triggers the default validation constraints (like @NotNull, @Size, @Email, etc.) on the object.
    • Used to validate nested objects (e.g., in an entity, it validates related objects if they are annotated with @Valid).
    • Supports validation on method arguments, request bodies, and fields.

Example:

@RestController
public class UserController {

    @PostMapping("/users")
    public ResponseEntity<String> createUser(@Valid @RequestBody User user) {
        return ResponseEntity.ok("User is valid");
    }
}

In this case, @Valid will ensure that the validation constraints on the User object are applied.

2. @Validated Annotation

  • Source: It is part of Spring Framework (org.springframework.validation.annotation.Validated).
  • Usage: Used when you need group validation or specific validation scenarios.
  • Functionality:
    • Supports validation groups for conditional validation.
    • Allows validation to be triggered only for certain groups of constraints.
    • Can be applied at the class level to enable validation on methods within the class (for method-level validation, which @Valid cannot do).

Example of Group Validation with @Validated:

public class User {

    @Null(groups = CreateGroup.class, message = "ID must be null for creation")
    @NotNull(groups = UpdateGroup.class, message = "ID is required for updating")
    private Long id;

    @NotBlank(message = "Name cannot be blank")
    private String name;
    
    // other fields
}
@RestController
public class UserController {

    @PostMapping("/users")
    public ResponseEntity<String> createUser(@Validated(CreateGroup.class) @RequestBody User user) {
        return ResponseEntity.ok("User created successfully");
    }

    @PutMapping("/users")
    public ResponseEntity<String> updateUser(@Validated(UpdateGroup.class) @RequestBody User user) {
        return ResponseEntity.ok("User updated successfully");
    }
}

Here, @Validated is used to apply different validation rules for creation and update scenarios, which is something @Valid does not support.

Key Differences:

Feature@Valid@Validated
OriginJava Bean Validation API (jakarta.validation.Valid)Spring Framework (org.springframework.validation.annotation.Validated)
UsageBasic validation without groupsAdvanced validation with groups
Validation GroupsNot supportedSupported
Method-level ValidationNot supportedSupported (when applied at the class level)
Nested Object ValidationSupportedSupported

When to Use:

  • Use **@Valid** for simple validations without the need for validation groups.
  • Use **@Validated** when you need more complex validation scenarios, such as group validation or method-level validation.

In summary, @Valid is generally used for basic validation scenarios, while @Validated is useful when you need more advanced control over validation, such as using validation groups or enabling method-level validation.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *