一、單個controller範圍的異常處理 說明: 在controller中加入被@ExceptionHandler修飾的類即可(在該註解中指定該方法需要處理的那些異常類) 該異常處理方法只在當前的controller中起作用 二、全部controller範圍內起作用的異常處理(全局異常處理) 1、 ...
一、單個controller範圍的異常處理
1 package com.xxx.secondboot.web; 2 3 import org.springframework.web.bind.annotation.ExceptionHandler; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestMethod; 6 import org.springframework.web.bind.annotation.RestController; 7 8 import com.xxx.secondboot.exception.MyExceptionResponse; 9 10 import io.swagger.annotations.Api; 11 12 @Api("測試controllerAdvice和全局異常處理") 13 @RestController 14 @RequestMapping("/advice1") 15 public class AdviceController { 16 17 @RequestMapping(value = "/test1", method = RequestMethod.GET) 18 public String test1() { 19 throw new RuntimeException("advice1 - exception1"); 20 } 21 22 @RequestMapping(value = "/test2", method = RequestMethod.GET) 23 public String test2() { 24 throw new RuntimeException("advice1 - exception2"); 25 } 26 27 @ExceptionHandler(RuntimeException.class) 28 public MyExceptionResponse exceptionHandler() { 29 MyExceptionResponse resp = new MyExceptionResponse(); 30 resp.setCode(300); 31 resp.setMsg("exception-Handler"); 32 return resp; 33 } 34 35 }
說明:
- 在controller中加入被@ExceptionHandler修飾的類即可(在該註解中指定該方法需要處理的那些異常類)
- 該異常處理方法只在當前的controller中起作用
二、全部controller範圍內起作用的異常處理(全局異常處理)
1、全局異常處理類
1 package com.xxx.secondboot.web; 2 3 import javax.servlet.http.HttpServletResponse; 4 5 import org.springframework.web.bind.annotation.ControllerAdvice; 6 import org.springframework.web.bind.annotation.ExceptionHandler; 7 import org.springframework.web.bind.annotation.ResponseBody; 8 import org.springframework.web.bind.annotation.RestController; 9 10 import com.xxx.secondboot.exception.MyExceptionResponse; 11 import com.xxx.secondboot.exception.MyRuntimeException; 12 13 //@ControllerAdvice(annotations=RestController.class) 14 //@ControllerAdvice(basePackages={"com.xxx","com.ooo"}) 15 @ControllerAdvice 16 public class GlobalExceptionHandler { 17 @ExceptionHandler(RuntimeException.class) 18 // @ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class}) 19 // @ExceptionHandler//處理所有異常 20 @ResponseBody //在返回自定義相應類的情況下必須有,這是@ControllerAdvice註解的規定 21 public MyExceptionResponse exceptionHandler(RuntimeException e, HttpServletResponse response) { 22 MyExceptionResponse resp = new MyExceptionResponse(); 23 resp.setCode(300); 24 resp.setMsg("exception-Handler"); 25 // response.setStatus(600); 26 return resp; 27 } 28 }
說明:
- @ControllerAdvice是controller的一個輔助類,最常用的就是作為全局異常處理的切麵類
- @ControllerAdvice可以指定掃描範圍
- @ControllerAdvice約定了幾種可行的返回值,如果是直接返回model類的話,需要使用@ResponseBody進行json轉換
- 返回String,表示跳到某個view
- 返回modelAndView
- 返回model + @ResponseBody
2、controller
1 package com.xxx.secondboot.web; 2 3 import org.springframework.web.bind.annotation.RequestMapping; 4 import org.springframework.web.bind.annotation.RequestMethod; 5 import org.springframework.web.bind.annotation.RestController; 6 7 import io.swagger.annotations.Api; 8 9 @Api("測試controllerAdvice和全局異常處理") 10 @RestController 11 @RequestMapping("/advice1") 12 public class AdviceController { 13 14 @RequestMapping(value = "/test1", method = RequestMethod.GET) 15 public String test1() { 16 throw new RuntimeException("advice1 - exception1"); 17 } 18 19 @RequestMapping(value = "/test2", method = RequestMethod.GET) 20 public String test2() { 21 throw new RuntimeException("advice1 - exception2"); 22 } 23 24 // @ExceptionHandler(RuntimeException.class) 25 // public MyExceptionResponse exceptionHandler() { 26 // MyExceptionResponse resp = new MyExceptionResponse(); 27 // resp.setCode(300); 28 // resp.setMsg("exception-Handler"); 29 // return resp; 30 // } 31 32 }
註意:
- 同一個異常被局部範圍異常處理器和全局範圍異常處理器同時覆蓋,會選擇小範圍的局部範圍處理器
- 同一個異常被小範圍的異常類和大範圍的異常處理器同時覆蓋,會選擇小範圍的異常處理器
參考自:
- http://jinnianshilongnian.iteye.com/blog/1866350 開濤的@ControllerAdvice(三個作用)
- http://www.tuicool.com/articles/fA7nuii springboot約定的異常處理體系
- https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc springMVC異常處理體系
- http://www.baeldung.com/2013/01/31/exception-handling-for-rest-with-spring-3-2/ springMVC異常處理體系