參考: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服務,都曾經歷過下面兩個問題:
- @PostMapping(path = "/selectByAcctcode")
- public MerAccountQueryResponse selectByAcctcode(@Valid @RequestBody MerAccountQueryRequest request,BindingResult result) {
-
- log.info(Constants.REQUEST_MSG, JSON.toJSONString(request));
-
- MerAccountQueryResponse response = new MerAccountQueryResponse();
-
- try {
- Pageable pageable = new PageRequest(request.getPageNum(), request.getPageSize());
- response = merAccountService.selectByAcctcode(request, pageable);
- // 返回成功報文
- MessageUtil.createCommMsg(response);
- } catch (BizException e) {
- log.error(Constants.BUSINESS_ERROR, e);
- // 組織錯誤報文
- MessageUtil.errRetrunInAction(response, e);
- } catch (Exception ex) {
- log.error(Constants.EXCEPTION_ERROR, ex);
- // 組織錯誤報文
- MessageUtil.createErrorMsg(response,ex);
- }
- log.info(Constants.REPONSE_MSG, JSON.toJSONString(response));
- return response;
- }
當你有100個接口的時候,就得重複100次,若是你以爲代碼量也就那麼點,copy就copy吧,反正我是代碼的搬運工,只是你有曾想過我能夠抽取出來嗎?
咱們在寫Controller的時候,若是沒有出現過異常當然沒問題,但一旦出現異常了,若是你處理了,那就須要你手動指定跳轉到事先定義好的界面,若是你沒處理,那將獲得是一個很是醜陋的界面,以下:
如何避免這種問題呢???
- @Controller
- @RequestMapping(value = "exception")
- public class ExceptionHandlerController {
-
- @ExceptionHandler({ ArithmeticException.class })
- public String handleArithmeticException(Exception e) {
- e.printStackTrace();
- return "error";
- }
-
- @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
- @ResponseBody
- public String testExceptionHandle(@PathVariable(value = "id") Integer id) {
- System.out.println(10 / id);
- return id.toString();
- }
- }
當訪問exception/e/0
的時候,會拋出ArithmeticException異常,@ExceptionHandler就會處理並響應error.jsp
- @Controller
- @RequestMapping(value = "exception")
- public class ExceptionHandlerController {
-
- @ExceptionHandler({ ArithmeticException.class })
- @ResponseBody
- public String handleArithmeticException(Exception e) {
- e.printStackTrace();
- JSONObject jo = new JSONObject();
- jo.put("resCode","999999");
- jo.put("resMsg","系統異常");
- return jo.toString();
- }
-
- @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
- @ResponseBody
- public String testExceptionHandle(@PathVariable(value = "id") Integer id) {
- System.out.println(10 / id);
- return id.toString();
- }
- }
固然實際項目中,並不會像我這裏寫的這麼簡陋,我這裏只是拋磚引玉,給你一個思路。
在實際項目中,可能碰到這種狀況,咱們提供的服務,調用方並不須要json報文中的消息,調用方只關注響應碼,好比200,表明調用正常;404,表明請求資源不存在;502,表明系統異常。。。等等。咱們又該如何去作?
- package com.somnus.exception;
-
- import org.springframework.http.HttpStatus;
- import org.springframework.web.bind.annotation.ResponseStatus;
-
- @ResponseStatus(value=HttpStatus.BAD_GATEWAY)
- public class HttpStatusException extends RuntimeException {
-
- private static final long serialVersionUID = 1L;
-
- public HttpStatusException() {
- super();
- }
-
- public HttpStatusException(String message, Throwable cause) {
- super(message, cause);
- }
-
- public HttpStatusException(String message) {
- super(message);
- }
-
- public HttpStatusException(Throwable cause) {
- super(cause);
- }
-
- }
- @Controller
- @RequestMapping(value = "status")
- public class ResponseStatusController {
-
- @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
- @ResponseBody
- public String status(@PathVariable(value = "id") Integer id){
- if(id % 2 != 0){
- throw new HttpStatusException();
- }
- return id.toString();
- }
- }
-
效果以下:
另外這裏不得不提一點須要注意的,不要輕易把@ResponseStatus修飾目標方法,由於不管它執行方法過程當中有沒有異常產生,用戶都會獲得異常的界面,而目標方法正常執行。
- package com.somnus.controller;
-
- import org.springframework.http.HttpStatus;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.bind.annotation.ResponseStatus;
-
- import com.somnus.exception.HttpStatusException;
-
- @Controller
- @RequestMapping(value = "status")
- public class ResponseStatusController {
-
- /**
- * ResponseStatus修飾目標方法,不管它執行方法過程當中有沒有異常產生,用戶都會獲得異常的界面。而目標方法正常執行
- * @param id
- * @return
- */
- @RequestMapping(value = "e2/{id}", method = { RequestMethod.GET })
- @ResponseStatus(value=HttpStatus.BAD_GATEWAY)
- @ResponseBody
- public String status2(@PathVariable(value = "id") Integer id){
- System.out.println(10 / id);
- return id.toString();
- }
-
- }
能夠看到哪怕是響應2了,可是響應碼其實仍是502
若是咱們要給jdk自帶的異常提供一個響應碼呢,咱們又不可能去改源碼,這時候@ResponseStatus就得配和@ControllerAdvice一塊兒使用了,以下:
- @Controller
- @RequestMapping(value = "exception")
- public class ExceptionHandlerController {
-
- @ExceptionHandler({ NullPointerException.class })
- @ResponseStatus(value=HttpStatus.NOT_FOUND)
- public void handleNullPointerException(Exception e) {
- e.printStackTrace();
- }
-
- @RequestMapping(value = "e3/{id}", method = { RequestMethod.GET })
- @ResponseBody
- public String testExceptionHandle3(@PathVariable(value = "id") Integer id) {
- List<String> list = 4 % id == 0 ? null : Arrays.asList(new String[]{"a","b","c","d"});
- return list.get(id);
- }
- }
當咱們拋出NullPointerException異常的時候會發生什麼呢
-
當一個Controller中有多個@ExceptionHandler註解出現時,那麼異常被哪一個方法捕捉呢?這就存在一個優先級的問題,@ExceptionHandler的優先級是:在異常的體系結構中,哪一個異常與目標方法拋出的異常血緣關係越緊密,就會被哪一個捕捉到
-
@ExceptionHandler這個只會是在當前的Controller裏面起做用,若是想在全部的Controller裏面統一處理異常的話,能夠用@ControllerAdvice來建立一個專門處理的類,咱們在下一篇作介紹。
@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
的初衷。