Tag Archives: @RestControllerAdvice

The @RestControllerAdvice annotation does not take effect in the Springboot project

Description:

When writing business logic in the back-end, you may encounter exceptions. The back-end usually throws an exception through throw, and then annotates the custom class with the @RestControllerAdvice annotation for unified processing, and the front-end parses the received results .

Exception handling class

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * error
     */
    @ExceptionHandler(BaseException.class)
    public ResultVo baseException(BaseException e) {
        log.error("base exception: {}", e.getMessage());
        return ResultVo.error(e.getMessage());
    }
}

Troubleshooting ideas

  1. Check whether the exception handling class is managed by Spring, @SpringbootApplication scans this package and sub-packages by default; if it is found, use @SpringbootApplication(scanBasePackages=”xxx.xxx”)
  2. Check the aspect programming in the project to see if an exception is try-catch in a certain aspect, and then it is not thrown out. It is common to wrap around the aspect, catching an exception and forgetting to throw it.

My question: Use the surround processing of the aspect to record the log. The log is divided into success, failure, and exception, and all exceptions are captured and processed.
Solution: After catching the exception, then throw the exception.