0
0
Spring Bootframework~20 mins

Validation error response formatting in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Validation Error Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Spring Boot validation error handler?
Given this controller advice method handling validation errors, what JSON response will it produce when a field "username" is empty?
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map handleValidationExceptions(MethodArgumentNotValidException ex) {
  Map errors = new HashMap<>();
  ex.getBindingResult().getAllErrors().forEach(error -> {
    String fieldName = ((FieldError) error).getField();
    String errorMessage = error.getDefaultMessage();
    errors.put(fieldName, errorMessage);
  });
  return errors;
}
A{"username": "must not be empty"}
B{"error": "Validation failed"}
C{"username": ""}
D{"message": "Field error"}
Attempts:
2 left
💡 Hint
Look at how the errors map is built from field names and messages.
📝 Syntax
intermediate
2:00remaining
Which option correctly formats a validation error response with a timestamp?
You want to include a timestamp and error details in your validation error response. Which code snippet correctly creates a JSON response with keys "timestamp" and "errors"?
A
Map&lt;String, Object&gt; body = new HashMap&lt;&gt;();
body.put("timestamp", LocalDateTime.now());
body.put("errors", errorsList);
return ResponseEntity.badRequest().body(body);
B
Map&lt;String, Object&gt; body = new HashMap&lt;&gt;();
body.put(timestamp, LocalDateTime.now());
body.put(errors, errorsList);
return ResponseEntity.badRequest().body(body);
C
Map&lt;String, String&gt; body = new HashMap&lt;&gt;();
body.put("timestamp", LocalDateTime.now().toString());
body.put("errors", errorsList.toString());
return ResponseEntity.ok().body(body);
D
Map body = new HashMap();
body.put("timestamp", LocalDateTime.now());
body.put("errors", errorsList);
return ResponseEntity.badRequest().body(body);
Attempts:
2 left
💡 Hint
Check for correct key quoting and response status.
🔧 Debug
advanced
2:00remaining
Why does this validation error handler cause a ClassCastException?
Consider this code snippet:
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map handleErrors(MethodArgumentNotValidException ex) {
  Map errors = new HashMap<>();
  for (Object error : ex.getBindingResult().getAllErrors()) {
    String field = ((FieldError) error).getField();
    String message = error.getDefaultMessage();
    errors.put(field, message);
  }
  return errors;
}

What is the most likely cause of a ClassCastException here?
AThe error.getDefaultMessage() returns null causing NullPointerException.
BThe map is declared with wrong generic types causing casting issues.
CSome errors in getAllErrors() are not FieldError instances, so casting fails.
DThe exception handler method lacks @ResponseStatus annotation causing runtime error.
Attempts:
2 left
💡 Hint
Not all errors are field errors in validation results.
state_output
advanced
2:00remaining
What is the JSON output of this custom validation error response?
Given this code snippet in a Spring Boot controller advice:
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map handleValidationErrors(MethodArgumentNotValidException ex) {
  List errors = ex.getBindingResult().getFieldErrors().stream()
    .map(err -> err.getField() + ": " + err.getDefaultMessage())
    .toList();
  Map response = new HashMap<>();
  response.put("status", 400);
  response.put("errors", errors);
  return response;
}

What will the JSON response look like if the "email" field is invalid with message "must be a valid email"?
A{"status":400,"error":"email must be a valid email"}
B{"status":400,"errors":{"email":"must be a valid email"}}
C{"status":"400","errors":["email must be a valid email"]}
D{"status":400,"errors":["email: must be a valid email"]}
Attempts:
2 left
💡 Hint
Look at how errors list is built with field and message concatenated with colon.
🧠 Conceptual
expert
3:00remaining
Which approach best supports internationalized validation error responses in Spring Boot?
You want your validation error messages to appear in the user's language automatically. Which approach below best supports this in Spring Boot?
AHardcode error messages in English inside the exception handler and translate manually before sending response.
BUse MessageSource with locale resolver and call messageSource.getMessage() inside the exception handler to get localized messages.
CUse @ResponseStatus with fixed messages on exception classes without localization support.
DReturn raw default messages from validation annotations without any localization logic.
Attempts:
2 left
💡 Hint
Spring Boot supports message localization via MessageSource and locale resolvers.