Quick revision
The short answer
Jakarta Bean Validation is a declarative standard: constraint annotations checked by ConstraintValidator classes, portable to any Java framework. Spring's Validator is a programmatic interface that writes into Spring's Errors model. Spring bridges them with LocalValidatorFactoryBean, which Spring Boot auto-configures and which implements both interfaces, so Bean Validation runs inside Spring's data binding and produces the same field errors. I choose a Spring Validator for cross-field rules specific to one form, rules that need Spring beans, or legacy code, and a constraint annotation for reusable field rules.
What to remember
In this answer, we compared Bean Validation, a portable declarative standard, with Spring's programmatic Validator tied to the Errors model. LocalValidatorFactoryBean implements both interfaces, so Bean Validation runs inside Spring data binding and its violations become ordinary field errors resolved by the MessageSource. We use annotations for reusable rules, Spring Validators for one-off cross-field rules, rules needing beans and legacy code, the 6.1 factory methods for small functional validators, and the service layer for rules that need database state.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Jakarta Bean Validation is a declarative standard: constraint annotations checked by ConstraintValidator classes, portable to any Java framework. Spring's Validator is a programmatic interface that writes into Spring's Errors model. Spring bridges them with LocalValidatorFactoryBean, which Spring Boot auto-configures and which implements both interfaces, so Bean Validation runs inside Spring's data binding and produces the same field errors. I choose a Spring Validator for cross-field rules specific to one form, rules that need Spring beans, or legacy code, and a constraint annotation for reusable field rules.
Why this question is asked
A Spring application often has both kinds of validation, and newcomers are not sure which to write or why both exist. The answer matters in real code: a reusable rule written as a one-off Spring Validator gets copied, and a one-form rule written as a custom annotation adds three classes for nothing. Interviewers ask this question to see whether we understand the bridge, because it explains why both kinds of errors show up in the same BindingResult. In this answer, we will recap each approach, look at how Spring connects them, compare when each fits, look at the newer functional factory methods, and finish with what belongs in neither.
1A Quick Recap of the Two Approaches
Bean Validation, now Jakarta Validation, is a specification with Hibernate Validator as its usual implementation. We declare rules with annotations such as @NotNull or @Size, or write our own annotation backed by a ConstraintValidator class. A jakarta.validation.Validator then checks an object and returns a set of ConstraintViolation objects. Nothing in it depends on Spring, so the same annotated class can be validated in a Jakarta EE server, a JavaFX app or a plain unit test.
Spring's org.springframework.validation.Validator predates that standard. It is a plain interface with supports and validate methods, and validate records problems in an Errors object with codes such as same.as.old, which Spring resolves to messages through its MessageSource. It is code, not annotations, so any logic is possible, but it only makes sense where Spring's Errors model is used.
| Jakarta Bean Validation | Spring Validator | |
|---|---|---|
| Style | declarative annotations | programmatic code |
| Result | Set<ConstraintViolation> | errors recorded in Errors |
| Portability | any Java framework | Spring only |
| Reuse | annotation on any field or class | registered per binder or called directly |
| Best for | reusable field and class rules | one-off cross-field rules, rules using beans |
BindingResult.2How Spring Bridges the Two
The bridge is LocalValidatorFactoryBean. It bootstraps the Bean Validation provider, and through its parent class SpringValidatorAdapter it implements both jakarta.validation.Validator and Spring's own Validator (more precisely SmartValidator, which adds support for validation groups). Spring Boot auto-configures one when a provider is on the classpath and uses it as the default validator for Spring MVC.
So when a controller parameter is marked @Valid, the WebDataBinder calls this adapter as an ordinary Spring Validator. The adapter runs Bean Validation, then converts each ConstraintViolation into a FieldError or ObjectError, with the annotation name as the error code. That is why a @Size violation and an error from our own Spring Validator sit side by side in the same BindingResult and are resolved by the same MessageSource.
Notice that the code of the converted error is Email, the annotation's simple name. Spring then expands it to Email.signupForm.email, Email.email and so on when it looks up the message, exactly as it does for a code we passed to rejectValue.
3When to Choose Which
A constraint annotation is the right tool for a rule that belongs to a type of value and is reused: an IBAN, a currency code, a postcode. It documents itself on the field, it is reported in OpenAPI by tools that read annotations, and it works outside Spring and in method validation on services.
A Spring Validator fits three other cases. The first is a cross-field rule that belongs to one form or use case, such as a new password differing from the old one, where a class-level annotation would be three classes for one rule. The second is a rule that needs Spring beans, for example a configured list of allowed file types, since the validator is an ordinary object we construct with dependencies. The third is existing code: older Spring MVC applications are full of Spring Validators, and rewriting them as annotations has little value on its own.
4Functional Validators Since Spring Framework 6.1
Spring Framework 6.1 added static factory methods that build a Validator from a lambda, which removes the boilerplate of supports and the cast, and a validateObject method that returns the Errors directly. Check your version before relying on them.
forInstanceOf matches the type and its subclasses, while forType matches the exact class only. These make Spring Validators pleasant for small rules and in tests, and validateObject is handy in functional endpoints, where there is no data binder.
5What Belongs in Neither
Rules that need database state, such as whether a username is already taken or whether an account has enough balance, should not live in either kind of validator. Validators run before the transaction that does the work, so the answer can be stale by the time we act on it, and putting repository calls in validators hides slow queries in a place nobody looks. These are business rules: they belong in the service layer, inside the transaction, and they fail with a typed exception. For uniqueness, the database unique constraint is the real guarantee, and the service translates its violation into a clear error.
6Follow-up questions
How do you report the same error format for both kinds?
Both end up as FieldError and ObjectError objects in a BindingResult, so one handler covers them. For a request body, both cause MethodArgumentNotValidException; for method validation, HandlerMethodValidationException or ConstraintViolationException. Our advice maps each of these to one ProblemDetail with an errors list of field, code and message, using the same MessageSource for the messages, so the client cannot tell which mechanism produced an error.
Can a ConstraintValidator use Spring beans?
Yes, when the validator factory is Spring's LocalValidatorFactoryBean. It uses a SpringConstraintValidatorFactory, which creates ConstraintValidator instances through the application context, so constructor injection works. The catch is that the constraint then only works inside Spring, which removes one of the reasons to use an annotation.
Does setValidator on a WebDataBinder disable Bean Validation?
Yes. setValidator replaces the binder's validators, including the default Bean Validation adapter. To add a Spring Validator while keeping annotations working, we call addValidators instead.
7Conclusion
In this answer, we compared Bean Validation, a portable declarative standard, with Spring's programmatic Validator tied to the Errors model. LocalValidatorFactoryBean implements both interfaces, so Bean Validation runs inside Spring data binding and its violations become ordinary field errors resolved by the MessageSource. We use annotations for reusable rules, Spring Validators for one-off cross-field rules, rules needing beans and legacy code, the 6.1 factory methods for small functional validators, and the service layer for rules that need database state.
Open with
Bean Validation is the Jakarta standard: I declare rules as annotations, custom ones are backed by a ConstraintValidator, and it works in any Java framework. Spring's Validator is older and programmatic: supports and validate, and it records errors with codes in Spring's Errors object.
Then, if they want more
The bridge is LocalValidatorFactoryBean, which Spring Boot auto-configures. Through SpringValidatorAdapter it implements both the Jakarta Validator and Spring's Validator. So when a controller parameter has @Valid, the data binder calls it like a Spring validator, it runs Bean Validation and converts each violation into a FieldError with the annotation name as the code.
That's why annotation errors and Spring Validator errors land in the same BindingResult and resolve through the same MessageSource. I use annotations for reusable field rules like IBAN or currency codes, because they document themselves and work outside Spring and in method validation.
I use a Spring Validator for a cross-field rule specific to one form, for rules that need Spring beans, or in legacy code. Since Spring 6.1 there are factory methods like Validator.forInstanceOf and validateObject that make small ones easy. Rules needing database state, like a taken username, go in neither; they belong in the service inside the transaction.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare Spring's Validator interface with Jakarta Bean Validation. How does Spring...": https://scrivio-interview-library.netlify.app/spring-boot/request-validation/e6573952a9/