The Validator interface in Spring (org.springframework.validation.Validator) is the framework's programmatic validation contract. You implement it to check an object in plain Java code and report problems through an Errors object. Spring MVC, data binding and your own services can all use it, and it works without Bean Validation annotations.
Spring also adapts Jakarta Bean Validation to this same interface, so annotation-based and hand-written validators can be mixed.
Validator compared with Bean ValidationValidator declares two methods.
supports tells Spring whether this validator can check instances of a given class.validate inspects the target and registers failures on the Errors object, using a field name, an error code and a default message.Since Spring Framework 6.1 there is also a default method validateObject(Object target) that returns an Errors instance, which is convenient outside web code.
This validator checks a registration form with rules that annotations express poorly, such as comparing two fields.
ValidationUtils has small helpers for the common empty checks. Use errors.reject(code, message) for an error that belongs to the whole object rather than one field.
You can call a validator from any code, using BeanPropertyBindingResult or SimpleErrors (added in Spring 6.1) as the Errors implementation.
Register the validator for a controller with @InitBinder. Spring then runs it whenever a parameter is validated, for example with @Valid on a @ModelAttribute.
Use addValidators rather than setValidator when you want to keep the default Bean Validation validator running alongside your own.
Validator compared with Bean Validation| Aspect | Spring Validator | Jakarta Bean Validation |
|---|---|---|
| Style | Imperative code | Declarative annotations |
| Cross-field logic | Easy | Needs a class-level constraint |
| Reuse in other frameworks | No, Spring only | Yes, standard |
| Error reporting | Errors and codes | ConstraintViolation |
Annotations are best for simple field rules. A Validator suits rules that depend on several fields, on database lookups, or on state that annotations cannot see. Spring bridges both through LocalValidatorFactoryBean, which implements Validator on top of Bean Validation.
supports to return true for subclasses?Usually yes. Use isAssignableFrom so subclasses and proxies of the target type are accepted.
Validator and @Valid work together?Yes. @Valid triggers validation, and Spring runs every validator registered on the WebDataBinder, including the default Bean Validation one when you use addValidators.
Pass error codes such as password.short and define them in a MessageSource properties file. The default message is only a fallback.
Validator only for web applications?No. It has no web dependency and can validate any object in services, batch jobs or tests.
Spring's Validator interface lets you write validation as code with supports and validate, reporting problems through Errors. Use it for cross-field or context-dependent rules, register it with @InitBinder in MVC, and combine it with Bean Validation annotations for simple constraints.