How to Handle Validation Errors in Spring Boot Correctly
In Spring Boot, handle validation errors by annotating your request object with
@Valid and catching errors with BindingResult or a global @ExceptionHandler. This lets you return clear error messages when input data fails validation rules.Why This Happens
Validation errors occur when the input data sent to your Spring Boot controller does not meet the rules defined by annotations like @NotNull or @Size. If you don't check for these errors properly, your app may crash or return unclear responses.
java
import jakarta.validation.constraints.NotBlank; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @PostMapping("/users") public String createUser(@RequestBody User user) { // No validation check here return "User created"; } } class User { @NotBlank private String name; // getters and setters public String getName() { return name; } public void setName(String name) { this.name = name; } }
Output
HTTP 200 OK with invalid data accepted or server error if validation fails silently
The Fix
Add @Valid to the request body parameter and include BindingResult to check for validation errors. Return a meaningful error response if validation fails.
java
import jakarta.validation.Valid; import jakarta.validation.constraints.NotBlank; import org.springframework.http.ResponseEntity; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @PostMapping("/users") public ResponseEntity<String> createUser(@Valid @RequestBody User user, BindingResult result) { if (result.hasErrors()) { return ResponseEntity.badRequest().body("Validation error: " + result.getFieldError().getDefaultMessage()); } return ResponseEntity.ok("User created"); } } class User { @NotBlank(message = "Name must not be blank") private String name; // getters and setters public String getName() { return name; } public void setName(String name) { this.name = name; } }
Output
HTTP 400 Bad Request with message: "Validation error: Name must not be blank" when name is empty
Prevention
Always use @Valid on input objects and check BindingResult or use a global @ControllerAdvice with @ExceptionHandler for MethodArgumentNotValidException. This keeps validation handling consistent and clean.
Use clear validation messages and test your API inputs to catch errors early.
Related Errors
- MethodArgumentNotValidException: Thrown when validation fails and not caught; handle it globally.
- ConstraintViolationException: Happens with validation on method parameters; handle with
@ExceptionHandler. - MissingServletRequestParameterException: When required request parameters are missing; handle with global exception advice.
Key Takeaways
Use @Valid on request objects to trigger validation in Spring Boot.
Check BindingResult for errors immediately after validation.
Return clear error messages with HTTP 400 status on validation failure.
Consider global exception handling with @ControllerAdvice for cleaner code.
Write meaningful validation messages to help API users fix input errors.