javaspringspring-mvcspring-aopcontroller-advice

@ControllerAdvice, how to get the class which called this method?


Is there a way to get the class from where the @ControllerAdvice got its control.

I.e.: If and execution of PersonController is going on and I get some error due to which the control transferred to the @ControllerAdvice class's method handleException(...).

Is there a way to get the PersonController class name inside the handleException method with Spring 3.2.3.

Any other way to achieve this?

Thank you for reading.


Solution

  • You can call the getStackTrace against your exception, first entry will give you the originating class

     handleException(YourException ex) {
        String exceptionController = ex.getStackTrace()[0].getClassName();
        ...
     }