0
0
Spring Bootframework~5 mins

@ControllerAdvice for global handling in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of @ControllerAdvice in Spring Boot?

@ControllerAdvice is used to handle exceptions and apply global configurations across multiple controllers in a Spring Boot application. It helps centralize error handling and common logic.

Click to reveal answer
beginner
How does @ExceptionHandler work inside a @ControllerAdvice class?
<p><code>@ExceptionHandler</code> methods inside a <code>@ControllerAdvice</code> class catch specific exceptions thrown by any controller and allow you to define a custom response or view for those errors.</p>
Click to reveal answer
intermediate
Can @ControllerAdvice be used to modify the response body for all controllers?

Yes, by combining @ControllerAdvice with @ResponseBody or returning ResponseEntity, you can modify or customize the response body globally for all controllers.

Click to reveal answer
intermediate
What is the difference between @ControllerAdvice and @RestControllerAdvice?

@RestControllerAdvice is a specialized version of @ControllerAdvice that combines it with @ResponseBody, so all methods return JSON or XML directly without needing to annotate each method.

Click to reveal answer
advanced
How do you limit @ControllerAdvice to specific controllers or packages?

You can use attributes like basePackages, assignableTypes, or annotations on @ControllerAdvice to restrict its scope to certain controllers or packages.

Click to reveal answer
What annotation do you use to create a global exception handler in Spring Boot?
A@Service
B@ControllerAdvice
C@Repository
D@Component
Which annotation inside @ControllerAdvice handles specific exceptions?
A@RequestMapping
B@GetMapping
C@ResponseBody
D@ExceptionHandler
What does @RestControllerAdvice automatically include?
A@ResponseBody
B@RequestBody
C@Service
D@Component
How can you restrict @ControllerAdvice to only certain packages?
AUsing <code>@Repository</code>
BUsing <code>@ComponentScan</code>
CUsing <code>basePackages</code> attribute
DUsing <code>@Service</code>
Where should you place @ControllerAdvice classes?
AAnywhere in the project, Spring will detect them
BOnly inside controller packages
COnly inside service packages
DOnly inside repository packages
Explain how @ControllerAdvice helps with global exception handling in Spring Boot.
Think about how one place can handle errors for many parts of your app.
You got /4 concepts.
    Describe the difference between @ControllerAdvice and @RestControllerAdvice.
    Focus on how the response is sent back to the client.
    You got /4 concepts.