@ControllerAdvice + @ExceptionHandler 捕獲全域性異常
來源:https://blog.csdn.net/w372426096/article/details/78429141
@ControllerAdvice
,是Spring3.2提供的新註解,從名字上可以看出大體意思是控制器增強。讓我們先看看@ControllerAdvice
的實現:
package org.springframework.web.bind.annotation;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ControllerAdvice {
@AliasFor("basePackages")
String[] value() default {};
@AliasFor("value")
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
Class<?>[] assignableTypes() default {};
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
一起使用的時候,就可以擺脫那個限制了。
- package com.somnus.advice;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseBody;
- @ControllerAdvice
- public class ExceptionAdvice {
- @ExceptionHandler({ ArrayIndexOutOfBoundsException.class })
- @ResponseBody
- public String handleArrayIndexOutOfBoundsException(Exception e) {
- e.printStackTrace();
- return "testArrayIndexOutOfBoundsException";
- }
- }
@Controller
@RequestMapping(value = "exception")
public class ExceptionHandlerController {
@RequestMapping(value = "e2/{id}", method = { RequestMethod.GET })
@ResponseBody
public String testExceptionHandle2(@PathVariable(value = "id") Integer id) {
List<String> list = Arrays.asList(new String[]{"a","b","c","d"});
return list.get(id-1);
}
}
當我們訪問
http://localhost:8080/SpringMVC/exception/e2/5
的時候會丟擲ArrayIndexOutOfBoundsException
異常,這時候定義在@ControllerAdvice
中的@ExceptionHandler
就開始發揮作用了。
如果我們想定義一個處理全域性的異常:
- package com.somnus.advice;
- import javax.servlet.http.HttpServletRequest;
- import org.springframework.core.annotation.AnnotationUtils;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.bind.annotation.ResponseStatus;
- @ControllerAdvice
- public class ExceptionAdvice {
- @ExceptionHandler({ Exception.class })
- @ResponseBody
- public String handException(HttpServletRequest request ,Exception e) throws Exception {
- e.printStackTrace();
- return e.getMessage();
- }
- }
乍一眼看上去毫無問題,但這裡有一個紕漏,由於Exception
是異常的父類,如果你的專案中出現過在自定義異常中使用@ResponseStatus
的情況,你的初衷是碰到那個自定義異常響應對應的狀態碼,而這個控制器增強處理類,會首先進入,並直接返回,不會再有@ResponseStatus
的事情了,這裡為了解決這種紕漏,我提供了一種解決方式。
- package com.somnus.advice;
- import javax.servlet.http.HttpServletRequest;
- import org.springframework.core.annotation.AnnotationUtils;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.bind.annotation.ResponseStatus;
- @ControllerAdvice
- public class ExceptionAdvice {
- @ExceptionHandler({ Exception.class })
- @ResponseBody
- public String handException(HttpServletRequest request ,Exception e) throws Exception {
- e.printStackTrace();
- //If the exception is annotated with @ResponseStatus rethrow it and let
- // the framework handle it - like the OrderNotFoundException example
- // at the start of this post.
- // AnnotationUtils is a Spring Framework utility class.
- if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null){
- throw e;
- }
- // Otherwise setup and send the user to a default error-view.
- /*ModelAndView mav = new ModelAndView();
- mav.addObject("exception", e);
- mav.addObject("url", request.getRequestURL());
- mav.setViewName(DEFAULT_ERROR_VIEW);
- return mav;*/
- return e.getMessage();
- }
- }
如果碰到了某個自定義異常加上了@ResponseStatus
,就繼續丟擲,這樣就不會讓自定義異常失去加上@ResponseStatus
的初衷。