๐Ÿ”ด The Error You're Seeing

Confirm this matches your console output. If it does, you're in the right place.

ERROR LOG2026-02-14 09:12:33.410 WARN 8842 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for object [userRequest]. Error count: 1] 2026-02-14 09:12:33.415 WARN 8842 --- [nio-8080-exec-1] o.s.web.method.annotation.MethodArgumentNotValidException : No message available

โšก Quick Fix Works 80% of the time

Ensure your DTO fields have proper validation annotations and the controller method parameter is annotated with @Valid.

@PostMapping public ResponseEntity<?> createUser(@Valid @RequestBody UserRequest userRequest) { // ... } public class UserRequest { @NotBlank(message = "Name is mandatory") private String name; }

๐Ÿ› ๏ธ Solutions (3 Ways to Fix)

Solution 1โœ“ Most common cause

Add @Valid to the @RequestBody parameter

๐Ÿ‘‰ Use this when you have validation annotations on your DTO but Spring isn't enforcing them.

Spring only triggers bean validation if you explicitly annotate the parameter with @Valid. Without it, the JSON is just mapped directly to the object without checks.

@PostMapping public ResponseEntity<?> createUser(@Valid @RequestBody UserRequest userRequest) { ... }
Solution 2

Add validation annotations to your DTO fields

๐Ÿ‘‰ Use this when @Valid is present but the bad data still gets through.

You must use Jakarta Validation annotations (like @NotBlank, @Size, @Email) inside your DTO class to define the rules.

public class UserRequest { @NotBlank(message = "Email is required") @Email(message = "Email should be valid") private String email; @Size(min = 8, message = "Password must be at least 8 chars") private String password; }
Solution 3

Handle the exception globally with @RestControllerAdvice

๐Ÿ‘‰ Use this when you want to return a clean JSON error response instead of a default 400 HTML page.

Create a global exception handler to catch MethodArgumentNotValidException and extract the specific field errors.

@RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<Map<String, String>> handleValidationExceptions(MethodArgumentNotValidException ex) { Map<String, String> errors = new HashMap<>(); ex.getBindingResult().getAllErrors().forEach(error -> { String fieldName = ((FieldError) error).getField(); String message = error.getDefaultMessage(); errors.put(fieldName, message); }); return ResponseEntity.badRequest().body(errors); } }

๐Ÿ“‹ Version Notes

Spring Boot 2.x

Uses javax.validation.* imports for validation annotations.

Spring Boot 3.x

Migrated to jakarta.validation.* imports. javax.validation will not compile.

๐Ÿง  Why this Happens

Tap to expand the deep technical explanation

Spring attempted to validate the incoming JSON payload against the constraints defined in your Java class, but the data failed one or more rules (e.g., a null field, or a string that was too short). Because the data is invalid, Spring rejects the request before it even reaches your controller logic.

The HITEC City Parking Spot Analogy:

Think of a bouncer at a club checking IDs. The @Valid annotation tells the bouncer to check the ID. If the ID is fake or missing (violates constraints), the bouncer rejects the person at the door before they can enter the club (your service layer).

๐Ÿ›ก๏ธ How to Prevent This Next Time

Always design your API contracts using DTOs with validation annotations, and never trust raw client input.

Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase