0
0
Spring Bootframework~10 mins

@ControllerAdvice for global handling 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 create a global exception handler class using @ControllerAdvice.

Spring Boot
import org.springframework.web.bind.annotation.[1];

@[1]
public class GlobalExceptionHandler {

}
Drag options to blanks, or click blank then click option'
AControllerAdvice
BRestController
CService
DComponent
Attempts:
3 left
💡 Hint
Common Mistakes
Using @RestController instead of @ControllerAdvice
Forgetting to import the annotation
Annotating with @Service or @Component which are not for exception handling
2fill in blank
medium

Complete the method annotation to handle exceptions of type NullPointerException globally.

Spring Boot
import org.springframework.web.bind.annotation.ExceptionHandler;

@ExceptionHandler([1].class)
public String handleNullPointer() {
    return "error-null";
}
Drag options to blanks, or click blank then click option'
AIOException
BIllegalArgumentException
CRuntimeException
DNullPointerException
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different exception type than intended
Omitting the .class suffix
Not annotating the method at all
3fill in blank
hard

Fix the error in the method signature to correctly handle exceptions globally.

Spring Boot
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpStatus;

@ExceptionHandler(Exception.class)
@ResponseStatus([1])
public String handleAllExceptions(Exception ex) {
    return "error";
}
Drag options to blanks, or click blank then click option'
AHttpStatus.INTERNAL_SERVER_ERROR
BHttpStatus.NOT_FOUND
CHttpStatus.OK
DHttpStatus.BAD_REQUEST
Attempts:
3 left
💡 Hint
Common Mistakes
Using HTTP 200 OK for error responses
Using HTTP 404 Not Found for exceptions
Not specifying any response status
4fill in blank
hard

Fill both blanks to create a method that handles IllegalArgumentException and returns a custom error message.

Spring Boot
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ExceptionHandler([1].class)
@ResponseBody
public String handleIllegalArgument([2] ex) {
    return "Invalid argument: " + ex.getMessage();
}
Drag options to blanks, or click blank then click option'
AIllegalArgumentException
BNullPointerException
DException
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching exception types between annotation and parameter
Forgetting to annotate with @ResponseBody for response content
Using a generic Exception type instead of specific
5fill in blank
hard

Fill all three blanks to create a global handler that returns a ResponseEntity with status and body.

Spring Boot
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.http.HttpStatus;

@ExceptionHandler([1].class)
public ResponseEntity<String> handleCustomException([2] ex) {
    return ResponseEntity.status([3]).body("Custom error: " + ex.getMessage());
}
Drag options to blanks, or click blank then click option'
ACustomException
CHttpStatus.BAD_REQUEST
DHttpStatus.NOT_FOUND
Attempts:
3 left
💡 Hint
Common Mistakes
Using different exception types in annotation and parameter
Returning plain String instead of ResponseEntity
Using wrong HTTP status codes