Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What is the purpose of the Validator interface in Spring?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 12, 2024
UpdatedNov 16, 2026
5 min read

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.

Table of Contents

  • The two methods of the interface
  • Writing a custom validator
  • Running a validator manually
  • Plugging a validator into Spring MVC
  • Validator compared with Bean Validation
  • Frequently Asked Questions
    • Do I need supports to return true for subclasses?
    • Can a Spring Validator and @Valid work together?
    • How do I show messages in different languages?
    • Is Validator only for web applications?
  • Summary

The two methods of the interface

Validator declares two methods.

java
  • 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.

Writing a custom validator

This validator checks a registration form with rules that annotations express poorly, such as comparing two fields.

java

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.

Running a validator manually

You can call a validator from any code, using BeanPropertyBindingResult or SimpleErrors (added in Spring 6.1) as the Errors implementation.

java
text

Plugging a validator into Spring MVC

Register the validator for a controller with @InitBinder. Spring then runs it whenever a parameter is validated, for example with @Valid on a @ModelAttribute.

java

Use addValidators rather than setValidator when you want to keep the default Bean Validation validator running alongside your own.

Validator compared with Bean Validation

AspectSpring ValidatorJakarta Bean Validation
StyleImperative codeDeclarative annotations
Cross-field logicEasyNeeds a class-level constraint
Reuse in other frameworksNo, Spring onlyYes, standard
Error reportingErrors and codesConstraintViolation

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.

Frequently Asked Questions

Do I need supports to return true for subclasses?

Usually yes. Use isAssignableFrom so subclasses and proxies of the target type are accepted.

Can a Spring 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.

How do I show messages in different languages?

Pass error codes such as password.short and define them in a MessageSource properties file. The default message is only a fallback.

Is Validator only for web applications?

No. It has no web dependency and can validate any object in services, batch jobs or tests.

Summary

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.

Share