Validations in Spring Framework
What is Validation?
Validation is the process of checking whether the input data is correct, complete, and meets certain rules or constraints before it's processed or saved.
In Java, and especially in Spring applications, validation ensures that:
A field is not
null
when it must have a value.A string meets minimum or maximum length.
A number is within a certain range.
An email address is properly formatted.
Custom business rules (like "age must be above 18") are enforced.
Validation is usually applied to:
Data received from users (form submissions, JSON payloads).
Objects used in APIs (DTOs).
Data models before saving them to the database.
Example of a simple constraint:
This example says: username should not be blank, and email must be in proper email format.
Why is Validation Important in Spring Applications?
Spring applications commonly deal with:
User input via APIs
Form data from websites
External data (like messages, files)
If data is not validated:
Invalid or incomplete data may reach your business logic or database.
It can cause runtime exceptions, data corruption, or security vulnerabilities.
It becomes harder to debug or trace what went wrong.
By validating early (at the controller level), we:
Prevent bad data from entering your system.
Return useful and specific error messages to the user.
Keep your core logic clean and focused on business rules.
Spring makes this process easy by integrating with Jakarta Bean Validation (like @NotNull
, @Size
, etc.) and automatically triggering checks.
Difference between Manual & Automatic (Declarative) Validation
Manual Validation
In manual validation, we write the code to check values and handle errors ourself.
Example:
This approach:
Requires more code
Is error-prone
Needs to be duplicated if used in many places
Offers no built-in grouping or nesting support
Automatic (Declarative) Validation
Spring uses declarative validation with annotations like @NotNull
, @Size
, @Valid
, etc.
We annotate the class fields, and Spring + Jakarta Validation will automatically check them.
Example:
Then in the controller:
We don't write if-checks — the framework handles it.
Advantages of Declarative Validation:
Less boilerplate code
Reusable annotations
Built-in internationalization of error messages
Supports groups, nesting, and custom constraints
Cleaner separation of validation rules from logic
Validation Types in Spring
Spring supports multiple ways of performing validation. Depending on the use case and complexity, we can choose:
Bean Validation (Jakarta Validation API –
jakarta.validation.*
)Spring’s own
Validator
interface (org.springframework.validation.Validator
)Custom Validation (annotations + logic)
Programmatic vs Declarative Validation
1. Bean Validation (Jakarta Validation API)
This is the most common and recommended approach in Spring applications.
Uses annotations from
jakarta.validation
(previouslyjavax.validation
) like@NotNull
,@Size
,@Email
,@Pattern
, etc.Spring integrates with Jakarta Validation and performs the checks automatically.
Used mostly in:
DTOs for REST APIs
Form submissions
Method parameters
Example:
In the controller:
Validation is recursive — if
UserDTO
contains another object with annotations, it is also validated using@Valid
.
Behind the scenes, Spring Boot uses Hibernate Validator as the default implementation of the Jakarta Bean Validation API.
2. Spring’s Validator
Interface
Validator
InterfaceThis is Spring’s native validation mechanism before it adopted Jakarta Bean Validation.
We implement the interface
org.springframework.validation.Validator
and manually define validation logic.Often used for:
Complex business validations that can't be captured with simple annotations
Older Spring applications
Manual validation within services/controllers
Example:
In controller:
This approach gives us full control but requires more boilerplate.
3. Custom Validation
This includes custom annotations and validation logic, usually built on top of Bean Validation.
Use when:
Built-in annotations aren’t sufficient (e.g., checking that a username is unique in the DB)
You want reusable annotations for business rules
Steps:
Create an annotation
Create a validator that implements
ConstraintValidator
Example:
Validator:
Use in DTO:
4. Programmatic vs Declarative Validation
Declarative Validation:
Uses annotations like
@NotNull
,@Valid
, etc.Cleaner and less code
Handled automatically by the framework
Example:
Spring automatically checks this and returns a 400 response if invalid.
Programmatic Validation:
Manual checks in Java code using
Validator
orif
conditionsOffers full control
More verbose
Example:
We inspect violations
and act accordingly.
When to use:
Use declarative for most use-cases (cleaner and standard).
Use programmatic only when:
We need dynamic constraints
We're writing libraries
We're validating inside services instead of controllers
Comparison
1. Bean Validation (Jakarta Validation API – jakarta.validation.*
)
jakarta.validation.*
)What it is
Standard Java validation via annotations (JSR-380 / Bean Validation 2.0).
Provided by
jakarta.validation
(formerly javax.validation
).
Annotations used
@NotNull
, @Size
, @Email
, @Min
, @Pattern
, etc.
Where it's used
DTOs, Entities, Request bodies.
Spring Integration
Deeply integrated — works automatically with @Valid
/ @Validated
in controllers.
Custom groups
Yes, supports groups
.
Nested validation
Yes, with @Valid
.
Best for
Common, field-level validation (input validation, simple rules).
2. Spring’s Validator
Interface (org.springframework.validation.Validator
)
Validator
Interface (org.springframework.validation.Validator
)What it is
Spring-specific interface for imperative, programmatic validation.
Method
Must implement validate(Object, Errors)
method.
No annotations
Logic is written in Java code, not annotations.
Use case
When you need full control over validation logic or want to validate objects not annotated.
Registration
Must be registered manually or via @InitBinder
.
Groups support
Manual.
Drawback
Verbose and manual.
3. Custom Validation (Annotations + Logic)
What it is
User-defined annotation + corresponding ConstraintValidator
.
Example
@ValidPassword
, @ValidUsername
, etc.
How
Define an annotation → implement validator → apply it to fields.
Powerful
Can include external service calls, database checks.
Reusable
Yes — once defined, works like any built-in annotation.
Spring aware
If needed, validator can be made Spring-aware via @Autowired
.
4. Programmatic vs Declarative Validation
How
Call validator manually in code
Use annotations on fields and classes
Example
validator.validate(object)
@NotBlank
, @Valid
, @Size
, etc.
Flexible?
Yes – full control
Less flexible, but cleaner
Verbose?
Yes
No – concise
When to use
For conditional logic, external checks
For field-level rules
When to Use What ?
Simple form field checks
Bean Validation (Jakarta)
Request DTO validation in controller
Jakarta + @Valid
/ @Validated
Cross-field or logic-based validation
Spring Validator
OR Custom ConstraintValidator
Need reusability and domain-specific rule
Custom Annotation + Validator
Complex condition-based logic
Programmatic validation
Different rules for create/update/view actions
Validation Groups + @Validated
Validation Groups
What are Validation Groups?
Validation groups allow us to apply different sets of constraints depending on the context.
For example:
When creating a user, we might want to validate fields like
username
,email
, andpassword
.When updating, we might only want to validate
email
(notpassword
orusername
).
Instead of duplicating your DTOs or writing custom logic, we can define groups and apply them using the groups
attribute on annotations.
Purpose of Validation Groups
Context-aware validation — apply only relevant constraints based on the use case (e.g., create vs update).
Reusability — same DTO with different validation behavior depending on operation.
Cleaner code — avoids manual condition-based checks.
How to Define and Use Validation Groups
1. Define Group Interfaces
These are just marker interfaces — no methods inside.
2. Annotate Fields with Group Info
Use the groups
parameter on constraints to specify where they should apply:
Explanation:
id
must be present only during updateusername
must be provided only during createemail
must be valid in both cases
3. Trigger Group-Specific Validation in Spring
In Spring MVC, use @Validated
instead of @Valid
to specify the group:
Last updated
Was this helpful?