@ControllerAdvice + @ExceptionHandler 捕获全局异常

来源:https://blog.csdn.net/w372426096/article/details/78429141

@ControllerAdvice,是Spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强。让我们先看看@ControllerAdvice的实现:

  1. package org.springframework.web.bind.annotation;
  2.  
  3. @Target(ElementType.TYPE)
  4. @Retention(RetentionPolicy.RUNTIME)
  5. @Documented
  6. @Component
  7. public @interface ControllerAdvice {
  8.  
  9. @AliasFor("basePackages")
  10. String[] value() default {};
  11.  
  12. @AliasFor("value")
  13. String[] basePackages() default {};
  14.  
  15. Class<?>[] basePackageClasses() default {};
  16.  
  17. Class<?>[] assignableTypes() default {};
  18.  
  19. Class<? extends Annotation>[] annotations() default {};

没什么特别之处,该注解使用@Component注解,这样的话当我们使用<context:component-scan>扫描时也能扫描到。

再一起看看官方提供的comment。

大致意思是:

  • @ControllerAdvice是一个@Component,用于定义@ExceptionHandler@InitBinder@ModelAttribute方法,适用于所有使用@RequestMapping方法。

  • Spring4之前,@ControllerAdvice在同一调度的Servlet中协助所有控制器。Spring4已经改变:@ControllerAdvice支持配置控制器的子集,而默认的行为仍然可以利用。

  • 在Spring4中, @ControllerAdvice通过annotations()basePackageClasses()basePackages()方法定制用于选择控制器子集。

不过据经验之谈,只有配合@ExceptionHandler最有用,其它两个不常用

这里写图片描述

在SpringMVC重要注解(一)@ExceptionHandler@ResponseStatus我们提到,如果单使用@ExceptionHandler,只能在当前Controller中处理异常。但当配合@ControllerAdvice一起使用的时候,就可以摆脱那个限制了

  1. package com.somnus.advice;
  2.  
  3. import org.springframework.web.bind.annotation.ControllerAdvice;
  4. import org.springframework.web.bind.annotation.ExceptionHandler;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6.  
  7. @ControllerAdvice
  8. public class ExceptionAdvice {
  9.  
  10. @ExceptionHandler({ ArrayIndexOutOfBoundsException.class })
  11. @ResponseBody
  12. public String handleArrayIndexOutOfBoundsException(Exception e) {
  13. e.printStackTrace();
  14. return "testArrayIndexOutOfBoundsException";
  15. }
  16.  
  17. }
 
  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4.  
  5. @RequestMapping(value = "e2/{id}", method = { RequestMethod.GET })
  6. @ResponseBody
  7. public String testExceptionHandle2(@PathVariable(value = "id") Integer id) {
  8. List<String> list = Arrays.asList(new String[]{"a","b","c","d"});
  9. return list.get(id-1);
  10. }
  11.  
  12. }
  13.  

当我们访问http://localhost:8080/SpringMVC/exception/e2/5的时候会抛出ArrayIndexOutOfBoundsException异常,这时候定义在@ControllerAdvice中的@ExceptionHandler就开始发挥作用了。

如果我们想定义一个处理全局的异常

  1. package com.somnus.advice;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4.  
  5. import org.springframework.core.annotation.AnnotationUtils;
  6. import org.springframework.web.bind.annotation.ControllerAdvice;
  7. import org.springframework.web.bind.annotation.ExceptionHandler;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import org.springframework.web.bind.annotation.ResponseStatus;
  10.  
  11. @ControllerAdvice
  12. public class ExceptionAdvice {
  13.  
  14. @ExceptionHandler({ Exception.class })
  15. @ResponseBody
  16. public String handException(HttpServletRequest request ,Exception e) throws Exception {
  17. e.printStackTrace();
  18.  
  19. return e.getMessage();
  20. }
  21.  
  22. }

乍一眼看上去毫无问题,但这里有一个纰漏,由于Exception是异常的父类,如果你的项目中出现过在自定义异常中使用@ResponseStatus的情况,你的初衷是碰到那个自定义异常响应对应的状态码,而这个控制器增强处理类,会首先进入,并直接返回,不会再有@ResponseStatus的事情了,这里为了解决这种纰漏,我提供了一种解决方式。

  1. package com.somnus.advice;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4.  
  5. import org.springframework.core.annotation.AnnotationUtils;
  6. import org.springframework.web.bind.annotation.ControllerAdvice;
  7. import org.springframework.web.bind.annotation.ExceptionHandler;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import org.springframework.web.bind.annotation.ResponseStatus;
  10.  
  11. @ControllerAdvice
  12. public class ExceptionAdvice {
  13.  
  14.  
  15. @ExceptionHandler({ Exception.class })
  16. @ResponseBody
  17. public String handException(HttpServletRequest request ,Exception e) throws Exception {
  18. e.printStackTrace();
  19. //If the exception is annotated with @ResponseStatus rethrow it and let
  20. // the framework handle it - like the OrderNotFoundException example
  21. // at the start of this post.
  22. // AnnotationUtils is a Spring Framework utility class.
  23. if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null){
  24. throw e;
  25. }
  26. // Otherwise setup and send the user to a default error-view.
  27. /*ModelAndView mav = new ModelAndView();
  28. mav.addObject("exception", e);
  29. mav.addObject("url", request.getRequestURL());
  30. mav.setViewName(DEFAULT_ERROR_VIEW);
  31. return mav;*/
  32. return e.getMessage();
  33. }
  34.  
  35. }


如果碰到了某个自定义异常加上了@ResponseStatus,就继续抛出,这样就不会让自定义异常失去加上@ResponseStatus的初衷。

猜你喜欢

转载自blog.csdn.net/wangpeng322/article/details/81912353