Spring boot validation annotations Valid and NotBlank not working
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The most common reason @Valid and @NotBlank do nothing in a Spring Boot application is a missing dependency. Starting with Spring Boot 2.3, the spring-boot-starter-validation dependency is no longer included transitively through spring-boot-starter-web. You must add it explicitly:
If the dependency is already present and validation still does not trigger, the problem is almost certainly one of the five other causes covered in this article: missing @Valid on the controller parameter, using the wrong import package, placing annotations on getters instead of fields, omitting @Validated on service classes, or a misconfigured MethodValidationPostProcessor. Each cause has a specific fix.
Cause 1: Missing spring-boot-starter-validation Dependency
Before Spring Boot 2.3, spring-boot-starter-web pulled in Hibernate Validator transitively through spring-boot-starter-web -> spring-boot-starter-tomcat -> hibernate-validator. That transitive path was removed in 2.3 to reduce unnecessary dependencies.
Maven
Gradle
After adding this, verify the dependency is resolved:
You should see hibernate-validator in the tree. If it is missing, validation annotations compile but have no runtime effect.
Cause 2: Missing @Valid on the Controller Method Parameter
The @NotBlank annotation on a field only defines a constraint. It does not trigger validation by itself. You must tell Spring to validate the incoming object by annotating the method parameter with @Valid:
Without @Valid, Spring deserializes the request body but skips validation entirely. This is the single most common cause after the dependency issue.
Cause 3: Wrong Import Package (javax vs jakarta)
Spring Boot 3.x migrated from Java EE (javax.*) to Jakarta EE (jakarta.*). Using the wrong package means annotations are present in the bytecode but the validator does not recognize them.
| Spring Boot Version | Correct Import |
| 2.x | javax.validation.constraints.NotBlank |
| 3.x | jakarta.validation.constraints.NotBlank |
If your IDE auto-imported the wrong package, validation compiles without errors but silently does nothing at runtime. Check every file that uses validation annotations.
Cause 4: Annotations on Getters Instead of Fields
Hibernate Validator's default access strategy validates fields, not getter methods. If your project uses field access (which is the default), placing @NotBlank on a getter will be ignored:
You can change the access strategy to PROPERTY using @AccessType, but that adds complexity. The simpler and more common convention is to annotate fields.
If you use Lombok's @Data or @Getter, annotations belong on the field, which Lombok leaves untouched when generating getters.
Cause 5: Missing @Validated on Service-Layer Classes
@Valid works automatically in Spring MVC controllers because Spring's RequestResponseBodyMethodProcessor handles it. In service classes, @Valid on method parameters does nothing unless the class is annotated with @Validated:
Without @Validated, the MethodValidationPostProcessor does not create a validation proxy around the service bean. The annotations exist but are never evaluated.
Cause 6: Nested Object Validation
When a request body contains nested objects, constraints on the nested object's fields are not validated unless the field is annotated with @Valid:
The @Valid on the shippingAddress field tells the validator to descend into the Address object and evaluate its constraints. This applies to collections as well:
Each OrderItem in the list will be individually validated.
Handling Validation Errors
When validation fails, Spring throws a MethodArgumentNotValidException for @RequestBody parameters. Without a handler, this returns a generic 400 response. Add an exception handler for clean error responses:
MethodArgumentNotValidException is thrown for @Valid @RequestBody failures. ConstraintViolationException is thrown for @Validated service-layer and path/query parameter failures.
Debugging Checklist
When validation is not working, walk through this checklist in order:
| Step | Check | Command/Action |
| 1 | Dependency present | mvn dependency:tree | grep hibernate-validator |
| 2 | @Valid on controller parameter | Inspect controller method signature |
| 3 | Correct import package | javax.* for Boot 2.x, jakarta.* for Boot 3.x |
| 4 | Annotations on fields, not getters | Inspect DTO/request classes |
| 5 | @Validated on service classes | Inspect service class annotations |
| 6 | @Valid on nested objects | Inspect nested DTO fields |
| 7 | Enable debug logging | Set logging.level.org.hibernate.validator=DEBUG |
Enabling Debug Logging
Add these properties to see exactly what the validator is doing:
This logs which constraints are evaluated, which objects are validated, and why specific validations pass or fail.
Common Pitfalls
Upgrading Spring Boot without adding the validation starter. This is the number one cause. Projects that worked fine on 2.2 silently lose validation after upgrading to 2.3+ because the transitive dependency was removed.
Mixing javax and jakarta imports. In a codebase migrating to Spring Boot 3, some files may still use javax imports. The code compiles, but constraints from the wrong package are invisible to the validator.
Using @Valid and @Validated interchangeably. They are not identical. @Valid (from the Bean Validation spec) triggers recursive validation but does not support groups. @Validated (Spring-specific) supports groups and is required for method-level validation in services.
Forgetting @Valid on nested objects. This is especially easy to miss in deeply nested request structures. Each level of nesting requires its own @Valid annotation.
Relying on @NotNull when @NotBlank is needed. @NotNull passes for empty strings (""). @NotBlank rejects both null and blank/whitespace-only strings. For user-facing text fields, @NotBlank is almost always the right choice.
Not handling validation exceptions. Without a @RestControllerAdvice handler, validation failures produce a generic 400 response with no useful detail. Always add a handler that maps field errors to a structured response.
Summary
- The most common fix is adding
spring-boot-starter-validationto your dependencies (required since Spring Boot 2.3). @Validmust appear on the controller method parameter to trigger validation, not just on the DTO fields.- Use
javax.validationimports for Spring Boot 2.x andjakarta.validationfor 3.x. - Place constraint annotations on fields, not getters.
- Add
@Validatedto service classes for method-level validation outside controllers. - Annotate nested object fields with
@Validfor recursive validation. - Always add an exception handler for
MethodArgumentNotValidExceptionto return useful error responses.
Related reading
- Spring Boot Value Properties
- Spring Boot version versus Spring Framework version?
- Spring Boot War deployed to Tomcat
- Spring boot Webclient's retrieve vs exchange
- Spring Boot with Kotlin - Value annotation not working as expected
- Spring Cloud AWS SQS fails to connect to service endpoint locally
- Spring Boot Websockets in Wildfly
- Spring Boot with Apache Tiles

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.