统一拦截controller异常
//@RestControllerAdvice
@ControllerAdvice({"com.my.learn.controller"})
public class ExceptionControllerAdvice {
@ResponseBody
@ExceptionHandler(MyRuntimeException.class)
public ReturnEntity handleException(MyRuntimeException e) {
System.out.println(" MyRuntimeException");
return new ReturnEntity(400, e.getMessage(), null);
}
@ResponseBody
@ExceptionHandler(RuntimeException.class)
public ReturnEntity handleException(RuntimeException e) {
System.out.println(" RuntimeException");
return new ReturnEntity(400, e.getMessage(), null);
}
@ResponseBody
@ExceptionHandler(Exception.class)
public ReturnEntity handleException(Exception e) {
System.out.println(" Exception");
return new ReturnEntity(400, e.getMessage(), null);
}
}
具体的异常拦截顺序,由spring自行处理,与方法顺序无关。