1. 程式人生 > >@ControllerAdvice + @ExceptionHandler 捕獲全域性異常

@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的初衷。