Spring Boot
Validation Annotations
@Valid
@NotBlank
Troubleshooting

Spring Boot @Valid and @NotBlank not working

Codemia Knowledge Hub ·

The most common reason @Valid and @NotBlank do nothing in Spring Boot is a missing dependency. Since Spring Boot 2.3, spring-boot-starter-web no longer brings in validation, so you must add spring-boot-starter-validation explicitly:

xml
1<dependency>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-validation</artifactId>
4</dependency>

If the dependency is present and validation still does not run, check for a missing @Valid on the controller parameter, the wrong import package (javax vs jakarta), constraints placed on both fields and getters, a missing @Validated on service classes, or a missing @Valid on nested objects.

Cause 1: missing spring-boot-starter-validation

Before Spring Boot 2.3, spring-boot-starter-web pulled in Hibernate Validator transitively. That was removed in 2.3 to reduce unnecessary dependencies, so projects that worked on 2.2 silently lose validation after upgrading.

Maven

xml
1<dependency>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-validation</artifactId>
4</dependency>

Gradle

gradle
implementation 'org.springframework.boot:spring-boot-starter-validation'

Then confirm it resolves:

bash
1# Maven
2mvn dependency:tree | grep validation
3
4# Gradle
5./gradlew dependencies | grep validation

You should see hibernate-validator in the tree. Without it, the annotations compile but have no runtime effect.

Cause 2: no @Valid on the controller parameter

@NotBlank on a field only defines a constraint. Spring validates the incoming object only when the method parameter carries @Valid:

java
1// WRONG: validation will not trigger
2@RestController
3@RequestMapping("/api/users")
4public class UserController {
5
6    @PostMapping
7    public ResponseEntity<User> createUser(@RequestBody UserRequest request) {
8        // @NotBlank on fields is ignored
9        return ResponseEntity.ok(userService.create(request));
10    }
11}
java
1// CORRECT: @Valid triggers bean validation
2@RestController
3@RequestMapping("/api/users")
4public class UserController {
5
6    @PostMapping
7    public ResponseEntity<User> createUser(@Valid @RequestBody UserRequest request) {
8        // @NotBlank is now enforced
9        return ResponseEntity.ok(userService.create(request));
10    }
11}
java
1public class UserRequest {
2    @NotBlank(message = "Name is required")
3    private String name;
4
5    @Email(message = "Email must be valid")
6    @NotBlank(message = "Email is required")
7    private String email;
8
9    // getters and setters
10}

Without @Valid, Spring deserializes the body and skips validation entirely. After the dependency, this is the most common cause.

Cause 3: javax vs jakarta imports

Spring Boot 3.x moved from Java EE (javax.*) to Jakarta EE (jakarta.*). Annotations from the wrong package are in the bytecode, but the validator does not recognize them.

Spring Boot VersionCorrect Import
2.xjavax.validation.constraints.NotBlank
3.xjakarta.validation.constraints.NotBlank
java
1// Spring Boot 2.x
2import javax.validation.Valid;
3import javax.validation.constraints.NotBlank;
4
5// Spring Boot 3.x
6import jakarta.validation.Valid;
7import jakarta.validation.constraints.NotBlank;

An IDE auto-import of the wrong package compiles without errors and silently does nothing. In a codebase migrating to Boot 3, check every file for leftover javax imports.

Cause 4: constraints on fields and getters

Bean Validation supports both field-level constraints (on the field) and property-level constraints (on the getter, not the setter). Pick one style per class and use it consistently. Annotating both a field and its getter validates the value twice.

java
1// Field-level: annotate the field
2public class UserRequest {
3    @NotBlank
4    private String name;
5
6    public String getName() {
7        return name;
8    }
9}
10
11// Property-level: annotate the getter instead (not both)
12public class UserRequest {
13    private String name;
14
15    @NotBlank
16    public String getName() {
17        return name;
18    }
19}

Annotating fields is the simpler and more common convention. With Lombok's @Data or @Getter, put the annotations on the field, which Lombok leaves untouched when it generates getters.

Cause 5: no @Validated on service classes

In Spring MVC controllers, @Valid works because RequestResponseBodyMethodProcessor handles it. In service classes, @Valid on a method parameter does nothing unless the class has @Validated:

java
1import org.springframework.validation.annotation.Validated;
2import jakarta.validation.Valid;
3import jakarta.validation.constraints.NotBlank;
4
5@Service
6@Validated  // Required for method-level validation in services
7public class UserService {
8
9    public User createUser(@Valid UserRequest request) {
10        // Validation now triggers via AOP proxy
11        return userRepository.save(mapToEntity(request));
12    }
13
14    public User findByName(@NotBlank String name) {
15        // @NotBlank on a simple parameter also works with @Validated
16        return userRepository.findByName(name);
17    }
18}

Without @Validated, MethodValidationPostProcessor creates no validation proxy around the bean, so the annotations are never evaluated.

@Valid and @Validated are not interchangeable. @Valid (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.

Cause 6: nested objects without @Valid

Constraints inside a nested object are checked only when the field holding it is annotated with @Valid:

java
1public class OrderRequest {
2    @NotBlank(message = "Order ID is required")
3    private String orderId;
4
5    @Valid  // Without this, Address constraints are ignored
6    @NotNull(message = "Shipping address is required")
7    private Address shippingAddress;
8}
9
10public class Address {
11    @NotBlank(message = "Street is required")
12    private String street;
13
14    @NotBlank(message = "City is required")
15    private String city;
16
17    @Pattern(regexp = "\\d{5}", message = "Zip code must be 5 digits")
18    private String zipCode;
19}

The same applies to collections, where each element is validated individually:

java
@Valid
@NotEmpty(message = "At least one item is required")
private List<OrderItem> items;

Each level of nesting needs its own @Valid, which is easy to miss in deep request structures.

@NotNull is not @NotBlank

@NotNull accepts empty strings (""). @NotBlank rejects null, empty and whitespace-only strings. For user-facing text fields, @NotBlank is almost always what you want.

Returning useful validation errors

A failed @Valid @RequestBody throws MethodArgumentNotValidException, which without a handler becomes a generic 400 with no useful detail. Map the errors to a structured response:

java
1@RestControllerAdvice
2public class ValidationExceptionHandler {
3
4    @ExceptionHandler(MethodArgumentNotValidException.class)
5    public ResponseEntity<Map<String, String>> handleValidationErrors(
6            MethodArgumentNotValidException ex) {
7        Map<String, String> errors = new HashMap<>();
8        ex.getBindingResult().getFieldErrors().forEach(error ->
9            errors.put(error.getField(), error.getDefaultMessage())
10        );
11        return ResponseEntity.badRequest().body(errors);
12    }
13
14    @ExceptionHandler(ConstraintViolationException.class)
15    public ResponseEntity<Map<String, String>> handleConstraintViolation(
16            ConstraintViolationException ex) {
17        Map<String, String> errors = new HashMap<>();
18        ex.getConstraintViolations().forEach(violation ->
19            errors.put(violation.getPropertyPath().toString(), violation.getMessage())
20        );
21        return ResponseEntity.badRequest().body(errors);
22    }
23}

ConstraintViolationException covers @Validated service-layer failures and path or query parameter failures.

Debugging checklist

StepCheckCommand/Action
1Dependency presentmvn dependency:tree | grep hibernate-validator
2@Valid on controller parameterInspect controller method signature
3Correct import packagejavax.* for Boot 2.x, jakarta.* for Boot 3.x
4Annotations on fields or getters, not bothInspect DTO/request classes
5@Validated on service classesInspect service class annotations
6@Valid on nested objectsInspect nested DTO fields
7Enable debug loggingSet logging.level.org.hibernate.validator=DEBUG

Debug logging shows which constraints are evaluated, which objects are validated, and why each validation passes or fails:

properties
logging.level.org.springframework.validation=DEBUG
logging.level.org.hibernate.validator=DEBUG

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms