javaspringspring-bootspring-restcontrollercontroller-advice

Is it possible to have mapping between rest controller and controller advice in spring boot 2.4.4 rest java 11 application?


I have a spring boot 2.4.4 java 11 application with many Rest Controllers. I am using @ControllerAdvice for handling all exceptions including custom exceptions. Is there anyway to map or identify which controller is involved at the time of execution? or Is there any way to map one @ControllerAdvice with one @RestController? Please advice. Thank you..


Solution

  • You can add which controller class you want to be handled using:

    @ControllerAdvice(basePackageClasses = {SomeController.class, AnotherController.class})
    

    Also, you can set a package:

    @ControllerAdvice(basePackages = {"com.myapp.controllers"})