Challenge - 5 Problems
Validation Mastery in Spring Boot
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when a Spring Boot controller receives invalid input?
Consider a Spring Boot REST controller method annotated with @Valid on its input DTO. What will the client receive if the input fails validation?
Spring Boot
public record UserDTO(@NotBlank String name, @Min(18) int age) {} @PostMapping("/users") public ResponseEntity<String> createUser(@Valid @RequestBody UserDTO user) { return ResponseEntity.ok("User created"); }
Attempts:
2 left
💡 Hint
Think about how Spring Boot handles @Valid and validation exceptions by default.
✗ Incorrect
Spring Boot automatically returns HTTP 400 Bad Request with details about validation errors when @Valid fails.
📝 Syntax
intermediate2:00remaining
Which code snippet correctly customizes validation error responses in Spring Boot?
You want to return a custom JSON structure for validation errors. Which code snippet correctly implements this using @ControllerAdvice?
Attempts:
2 left
💡 Hint
Look for the method signature that catches the right exception and returns a detailed map.
✗ Incorrect
Option B correctly uses @ExceptionHandler for MethodArgumentNotValidException and builds a map of field errors.
🔧 Debug
advanced2:00remaining
Why does this Spring Boot validation error handler not work as expected?
Given this code, why does the custom validation error handler never get called?
Spring Boot
@ControllerAdvice public class CustomValidationHandler { @ExceptionHandler(BindException.class) public ResponseEntity<String> handleBind(BindException ex) { return ResponseEntity.badRequest().body("Bind error"); } } @PostMapping("/submit") public ResponseEntity<String> submit(@Valid @RequestBody UserDTO user) { return ResponseEntity.ok("Success"); }
Attempts:
2 left
💡 Hint
Check which exception Spring Boot throws for @Valid on @RequestBody parameters.
✗ Incorrect
Spring Boot throws MethodArgumentNotValidException for @Valid on @RequestBody, so the handler for BindException is never called.
❓ state_output
advanced2:00remaining
What is the JSON response body when a validation error occurs with this handler?
Given this handler, what JSON will the client receive if the 'email' field is empty?
Spring Boot
@ControllerAdvice public class ValidationErrorHandler { @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<Map<String, String>> handleValidationErrors(MethodArgumentNotValidException ex) { Map<String, String> errors = new HashMap<>(); ex.getBindingResult().getFieldErrors().forEach(error -> errors.put(error.getField(), error.getDefaultMessage())); return ResponseEntity.badRequest().body(errors); } } // UserDTO with @NotBlank on 'email' field
Attempts:
2 left
💡 Hint
The handler builds a map with field names and their error messages.
✗ Incorrect
The handler returns a JSON object with the field name as key and the validation message as value.
🧠 Conceptual
expert3:00remaining
Which statement about Spring Boot validation error handling is true?
Select the correct statement about how Spring Boot handles validation errors by default and how to customize them.
Attempts:
2 left
💡 Hint
Think about default behavior and how to override it cleanly.
✗ Incorrect
Spring Boot returns HTTP 400 with a default error response for validation failures. To customize, use @ControllerAdvice and handle MethodArgumentNotValidException.