统一拦截controller异常

    1. //@RestControllerAdvice
    2. @ControllerAdvice({"com.my.learn.controller"})
    3. public class ExceptionControllerAdvice {
    4. @ResponseBody
    5. @ExceptionHandler(MyRuntimeException.class)
    6. public ReturnEntity handleException(MyRuntimeException e) {
    7. System.out.println(" MyRuntimeException");
    8. return new ReturnEntity(400, e.getMessage(), null);
    9. }
    10. @ResponseBody
    11. @ExceptionHandler(RuntimeException.class)
    12. public ReturnEntity handleException(RuntimeException e) {
    13. System.out.println(" RuntimeException");
    14. return new ReturnEntity(400, e.getMessage(), null);
    15. }
    16. @ResponseBody
    17. @ExceptionHandler(Exception.class)
    18. public ReturnEntity handleException(Exception e) {
    19. System.out.println(" Exception");
    20. return new ReturnEntity(400, e.getMessage(), null);
    21. }
    22. }


    具体的异常拦截顺序,由spring自行处理,与方法顺序无关。