Challenge - 5 Problems
Validation Error Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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 MaphandleValidationExceptions(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; }
Attempts:
2 left
💡 Hint
Look at how the errors map is built from field names and messages.
✗ Incorrect
The method collects all field errors and maps each field name to its error message. For an empty username, the message is typically "must not be empty".
📝 Syntax
intermediate2: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"?
Attempts:
2 left
💡 Hint
Check for correct key quoting and response status.
✗ Incorrect
Option A uses quoted keys, correct types, and returns a 400 Bad Request with the body map. Option A misses quotes causing compile errors. Option A returns 200 OK and uses string conversions which is less ideal. Option A uses raw types which is discouraged.
🔧 Debug
advanced2:00remaining
Why does this validation error handler cause a ClassCastException?
Consider this code snippet:
What is the most likely cause of a ClassCastException here?
@ExceptionHandler(MethodArgumentNotValidException.class) public MaphandleErrors(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?
Attempts:
2 left
💡 Hint
Not all errors are field errors in validation results.
✗ Incorrect
getAllErrors() returns ObjectError instances, some of which may not be FieldError. Casting all to FieldError causes ClassCastException.
❓ state_output
advanced2:00remaining
What is the JSON output of this custom validation error response?
Given this code snippet in a Spring Boot controller advice:
What will the JSON response look like if the "email" field is invalid with message "must be a valid email"?
@ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public MaphandleValidationErrors(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"?
Attempts:
2 left
💡 Hint
Look at how errors list is built with field and message concatenated with colon.
✗ Incorrect
The errors list contains strings like "field: message". The status is an integer 400. So the JSON has a numeric status and an array of error strings.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Spring Boot supports message localization via MessageSource and locale resolvers.
✗ Incorrect
Using MessageSource with locale resolver allows fetching messages in the user's language dynamically. Hardcoding or ignoring localization does not support internationalization.