SpringMVC異常處理註解@ExceptionHandler@ControllerAdvice@ResponseStatus

參考:java

http://blog.csdn.net/w372426096/article/details/78429132web

http://blog.csdn.net/w372426096/article/details/78429141spring

@ExceptionHandler:統一處理某一類異常,從而可以減小代碼重複率和複雜度json

源碼以下:restful

複製代碼
1 @Target({ElementType.METHOD})
2 @Retention(RetentionPolicy.RUNTIME)
3 @Documented
4 public @interface ExceptionHandler {
5     Class<? extends Throwable>[] value() default {};
6 }
複製代碼

註解做用對象爲方法,而且在運行時有效,value()能夠指定異常類。由該註解註釋的方法能夠具備靈活的輸入參數(詳細參見Spring API):app

  • 異常參數:包括通常的異常或特定的異常(即自定義異常),若是註解沒有指定異常類,會默認進行映射。
  • 請求或響應對象 (Servlet API or Portlet API): 你能夠選擇不一樣的類型,如ServletRequest/HttpServletRequest或PortleRequest/ActionRequest/RenderRequest
  • Session對象(Servlet API or Portlet API): HttpSession或PortletSession。
  • WebRequest或NativeWebRequest 
  • Locale
  • InputStream/Reader 
  • OutputStream/Writer 
  • Model

方法返回值能夠爲:jsp

  • ModelAndView對象
  • Model對象
  • Map對象
  • View對象
  • String對象
  • 還有@ResponseBody、HttpEntity<?>或ResponseEntity<?>,以及void

@ControllerAdvice

源碼以下:post

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

該註解做用對象爲TYPE,包括類、接口和枚舉等,在運行時有效,而且能夠經過Spring掃描爲bean組件。其能夠包含由@ExceptionHandler、@InitBinder 和@ModelAttribute標註的方法,能夠處理多個Controller類,這樣全部控制器的異常能夠在一個地方進行處理this

@ResponseStatus:能夠將某種異常映射爲HTTP狀態碼url

 

 


不管是普通的WEB項目,仍是用SpringMVC實現的restful服務,都曾經歷過下面兩個問題:

這裏寫圖片描述

 

  1. @PostMapping(path = "/selectByAcctcode")
  2. public MerAccountQueryResponse selectByAcctcode(@Valid @RequestBody MerAccountQueryRequest request,BindingResult result) {
  3.  
  4. log.info(Constants.REQUEST_MSG, JSON.toJSONString(request));
  5.  
  6. MerAccountQueryResponse response = new MerAccountQueryResponse();
  7.  
  8. try {
  9. Pageable pageable = new PageRequest(request.getPageNum(), request.getPageSize());
  10. response = merAccountService.selectByAcctcode(request, pageable);
  11. // 返回成功報文
  12. MessageUtil.createCommMsg(response);
  13. } catch (BizException e) {
  14. log.error(Constants.BUSINESS_ERROR, e);
  15. // 組織錯誤報文
  16. MessageUtil.errRetrunInAction(response, e);
  17. } catch (Exception ex) {
  18. log.error(Constants.EXCEPTION_ERROR, ex);
  19. // 組織錯誤報文
  20. MessageUtil.createErrorMsg(response,ex);
  21. }
  22. log.info(Constants.REPONSE_MSG, JSON.toJSONString(response));
  23. return response;
  24. }

 

當你有100個接口的時候,就得重複100次,若是你以爲代碼量也就那麼點,copy就copy吧,反正我是代碼的搬運工,只是你有曾想過我能夠抽取出來嗎?

 

這裏寫圖片描述

咱們在寫Controller的時候,若是沒有出現過異常當然沒問題,但一旦出現異常了,若是你處理了,那就須要你手動指定跳轉到事先定義好的界面,若是你沒處理,那將獲得是一個很是醜陋的界面,以下:

這裏寫圖片描述

如何避免這種問題呢???

這裏寫圖片描述

這裏寫圖片描述

 

  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4.  
  5. @ExceptionHandler({ ArithmeticException.class })
  6. public String handleArithmeticException(Exception e) {
  7. e.printStackTrace();
  8. return "error";
  9. }
  10.  
  11. @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
  12. @ResponseBody
  13. public String testExceptionHandle(@PathVariable(value = "id") Integer id) {
  14. System.out.println(10 / id);
  15. return id.toString();
  16. }
  17. }


當訪問exception/e/0的時候,會拋出ArithmeticException異常,@ExceptionHandler就會處理並響應error.jsp

這裏寫圖片描述

 

  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4.  
  5. @ExceptionHandler({ ArithmeticException.class })
  6. @ResponseBody
  7. public String handleArithmeticException(Exception e) {
  8. e.printStackTrace();
  9. JSONObject jo = new JSONObject();
  10. jo.put("resCode","999999");
  11. jo.put("resMsg","系統異常");
  12. return jo.toString();
  13. }
  14.  
  15. @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
  16. @ResponseBody
  17. public String testExceptionHandle(@PathVariable(value = "id") Integer id) {
  18. System.out.println(10 / id);
  19. return id.toString();
  20. }
  21. }

 

固然實際項目中,並不會像我這裏寫的這麼簡陋,我這裏只是拋磚引玉,給你一個思路。

這裏寫圖片描述

在實際項目中,可能碰到這種狀況,咱們提供的服務,調用方並不須要json報文中的消息,調用方只關注響應碼,好比200,表明調用正常;404,表明請求資源不存在;502,表明系統異常。。。等等。咱們又該如何去作?

這裏寫圖片描述

  1. package com.somnus.exception;
  2.  
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.web.bind.annotation.ResponseStatus;
  5.  
  6. @ResponseStatus(value=HttpStatus.BAD_GATEWAY)
  7. public class HttpStatusException extends RuntimeException {
  8.  
  9. private static final long serialVersionUID = 1L;
  10.  
  11. public HttpStatusException() {
  12. super();
  13. }
  14.  
  15. public HttpStatusException(String message, Throwable cause) {
  16. super(message, cause);
  17. }
  18.  
  19. public HttpStatusException(String message) {
  20. super(message);
  21. }
  22.  
  23. public HttpStatusException(Throwable cause) {
  24. super(cause);
  25. }
  26.  
  27. }
  1. @Controller
  2. @RequestMapping(value = "status")
  3. public class ResponseStatusController {
  4.  
  5. @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
  6. @ResponseBody
  7. public String status(@PathVariable(value = "id") Integer id){
  8. if(id % 2 != 0){
  9. throw new HttpStatusException();
  10. }
  11. return id.toString();
  12. }
  13. }

 

 

效果以下:

這裏寫圖片描述

另外這裏不得不提一點須要注意的,不要輕易把@ResponseStatus修飾目標方法,由於不管它執行方法過程當中有沒有異常產生,用戶都會獲得異常的界面,而目標方法正常執行。

  1. package com.somnus.controller;
  2.  
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import org.springframework.web.bind.annotation.ResponseStatus;
  10.  
  11. import com.somnus.exception.HttpStatusException;
  12.  
  13. @Controller
  14. @RequestMapping(value = "status")
  15. public class ResponseStatusController {
  16.  
  17. /**
  18. * ResponseStatus修飾目標方法,不管它執行方法過程當中有沒有異常產生,用戶都會獲得異常的界面。而目標方法正常執行
  19. * @param id
  20. * @return
  21. */
  22. @RequestMapping(value = "e2/{id}", method = { RequestMethod.GET })
  23. @ResponseStatus(value=HttpStatus.BAD_GATEWAY)
  24. @ResponseBody
  25. public String status2(@PathVariable(value = "id") Integer id){
  26. System.out.println(10 / id);
  27. return id.toString();
  28. }
  29.  
  30. }

 

 

能夠看到哪怕是響應2了,可是響應碼其實仍是502

這裏寫圖片描述


這裏寫圖片描述

若是咱們要給jdk自帶的異常提供一個響應碼呢,咱們又不可能去改源碼,這時候@ResponseStatus就得配和@ControllerAdvice一塊兒使用了,以下:

 

  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4.  
  5. @ExceptionHandler({ NullPointerException.class })
  6. @ResponseStatus(value=HttpStatus.NOT_FOUND)
  7. public void handleNullPointerException(Exception e) {
  8. e.printStackTrace();
  9. }
  10.  
  11. @RequestMapping(value = "e3/{id}", method = { RequestMethod.GET })
  12. @ResponseBody
  13. public String testExceptionHandle3(@PathVariable(value = "id") Integer id) {
  14. List<String> list = 4 % id == 0 ? null : Arrays.asList(new String[]{"a","b","c","d"});
  15. return list.get(id);
  16. }
  17. }

 

當咱們拋出NullPointerException異常的時候會發生什麼呢

這裏寫圖片描述

這裏寫圖片描述

    • 當一個Controller中有多個@ExceptionHandler註解出現時,那麼異常被哪一個方法捕捉呢?這就存在一個優先級的問題,@ExceptionHandler的優先級是:在異常的體系結構中,哪一個異常與目標方法拋出的異常血緣關係越緊密,就會被哪一個捕捉到

    • @ExceptionHandler這個只會是在當前的Controller裏面起做用,若是想在全部的Controller裏面統一處理異常的話,能夠用@ControllerAdvice來建立一個專門處理的類,咱們在下一篇作介紹。

 

@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. }



 

當咱們訪問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的初衷。

相關文章
相關標籤/搜索