@Validated

`@Validated` is a Spring annotation that enables Bean Validation integration and triggers validation of method arguments, return values, and other supported targets. Unlike `@Valid`, it also supports validation groups, allowing different validation rules to be applied in different scenarios.

`@Validated` is provided by Spring and works with the Bean Validation API. It is commonly placed on classes or method parameters to instruct Spring to perform validation before business logic executes.

A common use case is validating controller inputs. When a request object contains constraints such as `@NotNull` or `@Size`, Spring can automatically check those constraints and report validation errors instead of allowing invalid data to reach application code.

One of the main advantages of `@Validated` is support for validation groups. Groups allow developers to organize constraints and apply only a selected subset during a specific operation, such as creating a resource versus updating it.

`@Validated` is also used for method-level validation. When applied to a Spring-managed bean, constraints on method parameters and return values can be evaluated when the method is invoked through the Spring container.

Validation succeeds only when all applicable constraints pass. If a constraint fails, Spring typically throws a validation-related exception that can be handled and converted into an appropriate error response.

Key points

Interview questions

What is the purpose of `@Validated` in Spring?

`@Validated` enables validation through Spring's integration with the Bean Validation API. When applied to supported targets, Spring evaluates declared constraints before continuing execution. It helps keep validation rules close to the data model and reduces manual checks. It is commonly used in controllers and services.

Why would you use validation groups with `@Validated`?

Validation groups allow different sets of constraints to be applied in different contexts. For example, one operation may require fields that another operation treats as optional. `@Validated` can activate a specific group so only the relevant constraints are evaluated. This helps avoid creating separate classes solely for validation differences.

How does method-level validation work with `@Validated`?

When `@Validated` is applied to a Spring-managed component, Spring can validate method parameters and return values during invocation. Constraints placed on parameters are checked before method execution. If validation fails, an exception is raised and the method logic is typically not executed. The mechanism relies on Spring-managed beans rather than direct object creation.

What happens when validation fails?

If a constraint is violated, Spring raises a validation-related exception. The exact exception depends on the validation scenario and framework integration. Applications often use exception handlers to convert validation failures into structured error responses. This prevents invalid data from reaching business logic.

When would you choose `@Validated` instead of `@Valid`?

A common reason is the need for validation groups. `@Validated` allows selecting which groups should be applied during validation. It is also frequently used when enabling method-level validation on Spring components. If group selection is unnecessary, either annotation may be suitable depending on the use case.