0
0
Spring Bootframework~10 mins

Validation error response formatting in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to annotate a field for validation.

Spring Boot
public class User {
    @[1](message = "Name cannot be empty")
    private String name;
}
Drag options to blanks, or click blank then click option'
ANotNull
BMax
CMin
DSize
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Size without specifying min and max
Using @Min or @Max on a String field
2fill in blank
medium

Complete the code to handle validation errors in a controller advice.

Spring Boot
@RestControllerAdvice
public class ValidationExceptionHandler {
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<Map<String, String>> handleValidationExceptions(
            [1] ex) {
        Map<String, String> errors = new HashMap<>();
        ex.getBindingResult().getFieldErrors().forEach(error ->
                errors.put(error.getField(), error.getDefaultMessage()));
        return ResponseEntity.badRequest().body(errors);
    }
}
Drag options to blanks, or click blank then click option'
AMethodArgumentNotValidException
BConstraintViolationException
CIllegalArgumentException
DValidationException
Attempts:
3 left
💡 Hint
Common Mistakes
Using ConstraintViolationException which is for different validation context
Using generic exceptions like IllegalArgumentException
3fill in blank
hard

Fix the error in the code to extract field errors correctly.

Spring Boot
ex.getBindingResult().get[1]().forEach(error ->
    errors.put(error.getField(), error.getDefaultMessage()));
Drag options to blanks, or click blank then click option'
AFieldErrorsList
BFieldError
CFieldErrors
DFieldErrorsCollection
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like getFieldError or getFieldErrorsList
Using singular form instead of plural
4fill in blank
hard

Fill both blanks to create a custom error response class with fields for message and timestamp.

Spring Boot
public class ErrorResponse {
    private String [1];
    private LocalDateTime [2];

    public ErrorResponse(String message, LocalDateTime timestamp) {
        this.message = message;
        this.timestamp = timestamp;
    }

    // getters and setters
}
Drag options to blanks, or click blank then click option'
Amessage
Berror
Ctimestamp
Dtime
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent field names that don't match constructor
Using generic names like 'error' or 'time' instead of 'message' and 'timestamp'
5fill in blank
hard

Fill all three blanks to build a method that returns a formatted error response with HTTP status and body.

Spring Boot
public ResponseEntity<ErrorResponse> buildErrorResponse(String [1], HttpStatus [2]) {
    ErrorResponse errorResponse = new ErrorResponse([1], LocalDateTime.now());
    return ResponseEntity.status([2]).body(errorResponse);
}
Drag options to blanks, or click blank then click option'
Amessage
Bstatus
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names that don't match method signature
Confusing the order of parameters