๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
2026-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)
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) { ... }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;
}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
Uses javax.validation.* imports for validation annotations.
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.