springmvc异常统一处理(一)
目录 使用 @ ExceptionHandler 注解 实现 HandlerExceptionResolver 接口 使用 @ControllerAdvice+ @ ExceptionHandler 注解 参考资料 正文 Spring 统一异常处理有 3 种方式,分别为: 使用 @ ExceptionHandler 注解 实现 HandlerExceptionResolver 接口 使用 @controlleradvice 注解 使用 @ ExceptionHandler 注解 使用该注解有一个不好的地方就是:进行异常处理的方法必须与出错的方法在同一个Controller里面。使用如下: 1 @Controller 2 public class GlobalController { 3 4 /** 5 * 用于处理异常的 6 * @return 7 */ 8 @ExceptionHandler({MyException.class}) 9 public String exception(MyException e) { 10 System.out.println(e.getMessage()); 11 e.printStackTrace(); 12 return "exception"; 13 } 14 15 @RequestMapping("test") 16 public