Validation with @Valid and Custom ConstraintValidator
Fast recall
Quick revision
Core answer
Practical knowledge of Bean Validation integration in Spring MVC and ability to write domain-specific validation rules.
Putting validation logic inside the service layer with manual if-checks instead of using constraint annotations that work across controllers, services, and JPA entities.
Explain the ConstraintValidatorContext API for custom message interpolation, mention cross-field validators using @Constraint on a class, and note that @Validated (Spring) supports group sequences while @Valid (Jakarta) does not.
Interview-ready guide
Interview answer
Bean Validation in Spring MVC validates request data before it reaches your service layer, keeping business logic clean and producing consistent 400 responses for invalid input.
How it wires together
- Add
@NotBlank,@NotNull,@Size,@Patternetc. on DTO fields - Add
@Validto the@RequestBodyparameter in the controller method - Spring calls the validator before invoking the controller method; on failure it throws
MethodArgumentNotValidException - Handle in
@ControllerAdvice:ex.getBindingResult().getAllErrors()gives every constraint violation
Custom constraints
- Create
@UniqueEmailannotation +UniqueEmailValidator implements ConstraintValidator isValid(String value, ConstraintValidatorContext ctx)— inject a repository and check uniqueness; Spring manages the validator as a bean- Cross-field validation: annotate the DTO class itself with a class-level constraint that receives the entire object
`@Valid` vs `@Validated`
@Validis the standard Java annotation — use for method parameters in controllers@Validatedis Spring's annotation — enables validation groups (validate different subsets of constraints for create vs update operations)
Cascade validation: a DTO with a nested object field must annotate that field with @Valid to propagate validation into the nested object — omitting it silently skips validation of nested constraints.
Validation with @Valid and Custom ConstraintValidator
Direct Answer
@Valid (or @Validated) on a controller parameter triggers Bean Validation on the request body or query object. For complex rules not covered by standard annotations, you implement ConstraintValidator with your own @Constraint annotation, keeping validation logic reusable, declarative, and testable independently of Spring.
Common Mistakes & Pitfalls
Putting validation logic inside the service layer with manual if-checks instead of using constraint annotations that work across controllers, services, and JPA entities.
Check your understanding
Answer these without looking back at the article.
- How do you write a cross-field constraint that validates two fields on the same DTO in relation to each other?
- What is the difference between @Valid and @Validated in Spring?
- How would you unit-test a ConstraintValidator without starting a Spring context?
Related interview prep categories
1Want a guided curriculum order?
These modules are also sequenced in our flagship backend interview track — open it when you prefer an ordered path instead of browsing by topic.