0
0
Spring Bootframework~10 mins

ResponseEntityExceptionHandler 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 extend the class for handling exceptions globally.

Spring Boot
public class GlobalExceptionHandler extends [1] { }
Drag options to blanks, or click blank then click option'
AException
BResponseEntityExceptionHandler
CRuntimeException
DControllerAdvice
Attempts:
3 left
💡 Hint
Common Mistakes
Extending Exception or RuntimeException instead of ResponseEntityExceptionHandler
Using @ControllerAdvice as a class to extend
2fill in blank
medium

Complete the annotation to mark the class as a global exception handler.

Spring Boot
@[1]
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { }
Drag options to blanks, or click blank then click option'
ARestController
BService
CControllerAdvice
DComponent
Attempts:
3 left
💡 Hint
Common Mistakes
Using @RestController instead of @ControllerAdvice
Forgetting to annotate the class
3fill in blank
hard

Fix the method signature to override the handler for MethodArgumentNotValidException.

Spring Boot
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
    MethodArgumentNotValidException [1],
    HttpHeaders headers, HttpStatus status, WebRequest request) {
    // method body
}
Drag options to blanks, or click blank then click option'
Aerror
Bexception
Ce
Dex
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter names that don't match the override signature
Missing the @Override annotation
4fill in blank
hard

Fill both blanks to create a ResponseEntity with a custom error message and BAD_REQUEST status.

Spring Boot
return new ResponseEntity<>([1], [2]);
Drag options to blanks, or click blank then click option'
AerrorDetails
BHttpStatus.BAD_REQUEST
CHttpStatus.NOT_FOUND
DresponseBody
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of arguments
Using wrong HTTP status codes
5fill in blank
hard

Fill all three blanks to build a map of field errors inside the exception handler.

Spring Boot
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getFieldErrors().forEach(error -> {
    errors.put(error.getField(), [1]);
});
return new ResponseEntity<>([2], [3]);
Drag options to blanks, or click blank then click option'
Aerror.getDefaultMessage()
Berrors
CHttpStatus.BAD_REQUEST
Derror.getCode()
Attempts:
3 left
💡 Hint
Common Mistakes
Using error code instead of default message
Returning wrong status code
Not returning the errors map