How to manually trigger Spring validation?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Spring MVC, validation often happens automatically when a controller parameter is annotated with @Valid or @Validated. But outside that request-binding flow, you may need to trigger validation yourself from a service, batch job, scheduled task, or custom workflow.
Spring supports this cleanly. The main question is whether you want Bean Validation through jakarta.validation.Validator or Spring's own Validator interface.
Manual Validation with Bean Validation
If your object uses annotations such as @NotBlank, @Email, or @Min, inject a Bean Validation Validator and call validate().
This is the most common manual-validation approach in Spring Boot applications.
Using Spring's Validator with Errors
If you have a custom Spring Validator, call it directly with a BindingResult implementation.
This style is useful when validation logic is custom and not expressed as bean annotations.
When Manual Validation Makes Sense
Manual validation is useful when the object is not being created by Spring MVC request binding. Typical examples include:
- messages read from a queue
- rows imported from a CSV file
- intermediate objects created inside a service
- validation before calling a downstream system
In those cases, relying on @Valid alone is not enough because no controller method is triggering the validation step for you.
Validation Groups and Reuse
Manual validation also gives you precise control over validation groups.
That is helpful when the same DTO has different rules for create versus update operations.
The broader design rule is simple: validation should live close to the boundary where incorrect data first becomes a problem. Sometimes that boundary is the controller. Sometimes it is a service or batch processor instead.
Common Pitfalls
- Assuming
@Validruns automatically everywhere. It only runs where Spring is actually managing the validation trigger. - Throwing away detailed violation information too early instead of mapping it into a useful error response.
- Mixing Bean Validation annotations and a custom Spring
Validatorwithout being clear about which one owns the rules. - Creating validation logic in controllers only, then forgetting to validate objects created elsewhere in the application.
- Manually validating but ignoring validation groups when different workflows need different constraints.
Summary
- Manual Spring validation is straightforward once you choose the right validator type.
- Use
jakarta.validation.Validatorfor annotation-based Bean Validation. - Use Spring's
ValidatorplusErrorsfor custom validation logic. - Manual validation is especially useful outside controller request binding.
- The goal is not just to "run validation," but to run it at the right application boundary.
Related reading
- How to map a composite key with JPA and Hibernate?
- How to mark a class as Deprecated?
- how to mark a message as persistent using spring-rabbitmq?
- How to measure service methods using spring boot 2 and micrometer
- How to migrate existing Spring project to Spring Boot
- How to mock a final class with mockito
- How to mock JWT authentication in a Spring Boot Unit Test?
- How to mock void methods with Mockito

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.