Javax validation on nested objects - not working
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Javax (Jakarta) Bean Validation does not automatically validate nested objects. If you have an Order with an Address field, adding @NotNull to Address fields does nothing unless the Address field itself is annotated with @Valid. This is the single most common reason nested validation "does not work" — the @Valid annotation is missing on the parent field.
The Problem
If you submit an Order with a non-null Address that has a blank street, validation passes. The @NotBlank on street is never checked because the validator does not descend into Address.
The Fix: Add @Valid
@Valid tells the validator to recursively validate the annotated object. Now @NotBlank on street and city, and @Size on zipCode are enforced.
@Valid on Collections
@Valid also works on collections to validate each element:
Without @Valid on the items list, individual LineItem constraints are ignored even if the list contains invalid items.
Spring MVC: @Valid on Controller Parameters
In Spring MVC, you must also use @Valid on the controller method parameter:
If you omit @Valid from the @RequestBody parameter, no validation runs at all — neither top-level nor nested.
@Validated vs @Valid
Spring provides @Validated as an alternative to @Valid with support for validation groups:
@Valid does not support groups — use @Validated when you need group-based validation.
Deeply Nested Objects
@Valid cascades recursively. If Address contains a Country object, add @Valid at each level:
If you forget @Valid on country, the Country constraints are skipped.
Programmatic Validation
You can also validate objects manually using Validator:
This only works if @Valid is on the shippingAddress field.
Dependency Requirements
Ensure you have the validation implementation on the classpath:
In Spring Boot 2.3+, spring-boot-starter-web no longer includes validation automatically. You must add spring-boot-starter-validation explicitly.
Jakarta vs Javax Namespace
Starting with Jakarta EE 9, the package changed from javax.validation to jakarta.validation:
If your annotations come from the wrong namespace, the validator ignores them entirely.
Common Pitfalls
- Missing
@Validon the nested field: The number one cause. Without@Valid, the validator skips nested object constraints entirely. - Missing
@Validon the controller parameter: In Spring MVC, the@RequestBodyparameter must be annotated with@Validor@Validatedto trigger validation. - Missing
spring-boot-starter-validation: Since Spring Boot 2.3, validation is not included inspring-boot-starter-web. Add the validation starter explicitly. - Wrong namespace (javax vs jakarta): Mixing
javax.validationannotations with a Jakarta validator (or vice versa) causes silent validation failure. @Validon a null field:@Validon a null field does not trigger validation errors for the nested object's fields — it simply skips validation. Combine with@NotNullto ensure the object exists.
Summary
- Add
@Validon nested object fields to enable cascading validation @Validworks on single objects, collections, and maps- In Spring MVC, annotate
@RequestBodyparameters with@Validor@Validated - Use
@Validated(Spring-specific) for validation groups - Add
spring-boot-starter-validationin Spring Boot 2.3+ - Check the namespace:
javax.validationfor Java EE / Spring Boot 2.x,jakarta.validationfor Jakarta EE / Spring Boot 3.x
Related reading
- javax vs java package
- javax.management.InstanceNotFoundException org.springframework.boottypeAdmin,nameSpringApplication
- javax.transaction.Transactional vs org.springframework.transaction.annotation.Transactional
- JAX-RS / Jersey how to customize error handling?
- Jenkins X error secrets jenkins not found
- Jersey stopped working with InjectionManagerFactory not found
- JComboBox Selection Change Listener?
- Jenkinsfile syntax highlighting in Java project using IntelliJ IDEA

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.