0
0
Spring Bootframework~20 mins

Validation error responses in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Validation Mastery in Spring Boot
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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");
}
AHTTP 500 Internal Server Error due to validation failure
BHTTP 200 OK with an empty response body
CHTTP 400 Bad Request with a JSON body listing validation errors
DHTTP 404 Not Found because the resource is missing
Attempts:
2 left
💡 Hint
Think about how Spring Boot handles @Valid and validation exceptions by default.
📝 Syntax
intermediate
2: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?
A
@ControllerAdvice
public class ValidationHandler {
  public ResponseEntity&lt;String&gt; handleValidation(MethodArgumentNotValidException ex) {
    return ResponseEntity.badRequest().body("Error");
  }
}
B
@ControllerAdvice
public class ValidationHandler {
  @ExceptionHandler(MethodArgumentNotValidException.class)
  public ResponseEntity&lt;Map&lt;String, String&gt;&gt; handleValidation(MethodArgumentNotValidException ex) {
    Map&lt;String, String&gt; errors = new HashMap&lt;&gt;();
    ex.getBindingResult().getFieldErrors().forEach(error -&gt;
      errors.put(error.getField(), error.getDefaultMessage()));
    return ResponseEntity.badRequest().body(errors);
  }
}
C
@RestController
public class ValidationHandler {
  @ExceptionHandler(MethodArgumentNotValidException.class)
  public String handleValidation() {
    return "Validation failed";
  }
}
D
@ControllerAdvice
public class ValidationHandler {
  @ExceptionHandler(Exception.class)
  public ResponseEntity&lt;String&gt; handleAll(Exception ex) {
    return ResponseEntity.status(500).body("Server error");
  }
}
Attempts:
2 left
💡 Hint
Look for the method signature that catches the right exception and returns a detailed map.
🔧 Debug
advanced
2: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");
}
AThe exception thrown is MethodArgumentNotValidException, not BindException, so the handler is not triggered
BThe @ControllerAdvice class is missing @RestController annotation
CThe @Valid annotation should be on the controller class, not the method parameter
DThe handler method must return void to work correctly
Attempts:
2 left
💡 Hint
Check which exception Spring Boot throws for @Valid on @RequestBody parameters.
state_output
advanced
2: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
A{"email":null}
B{"error":"Validation failed"}
C{"message":"Invalid input"}
D{"email":"must not be blank"}
Attempts:
2 left
💡 Hint
The handler builds a map with field names and their error messages.
🧠 Conceptual
expert
3: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.
ASpring Boot automatically returns HTTP 400 with a default error body on validation failure; customizing requires @ControllerAdvice with @ExceptionHandler for MethodArgumentNotValidException
BSpring Boot returns HTTP 500 on validation errors unless you add @ResponseStatus on the DTO class
CValidation errors are ignored by default and must be manually checked in controller methods
DYou must implement HandlerInterceptor to catch validation errors globally
Attempts:
2 left
💡 Hint
Think about default behavior and how to override it cleanly.