一、單個controller范圍的異常處理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package com.xxx.secondboot.web; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.xxx.secondboot.exception.MyExceptionResponse; import io.swagger.annotations.Api; @Api ( "測試controllerAdvice和全局異常處理" ) @RestController @RequestMapping ( "/advice1" ) public class AdviceController { @RequestMapping (value = "/test1" , method = RequestMethod.GET) public String test1() { throw new RuntimeException( "advice1 - exception1" ); } @RequestMapping (value = "/test2" , method = RequestMethod.GET) public String test2() { throw new RuntimeException( "advice1 - exception2" ); } @ExceptionHandler (RuntimeException. class ) public MyExceptionResponse exceptionHandler() { MyExceptionResponse resp = new MyExceptionResponse(); resp.setCode( 300 ); resp.setMsg( "exception-Handler" ); return resp; } } |
說明:
- 在controller中加入被@ExceptionHandler修飾的類即可(在該注解中指定該方法需要處理的那些異常類)
- 該異常處理方法只在當前的controller中起作用
二、全部controller范圍內起作用的異常處理(全局異常處理)
1、全局異常處理類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package com.xxx.secondboot.web; import javax.servlet.http.HttpServletResponse; 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.RestController; import com.xxx.secondboot.exception.MyExceptionResponse; import com.xxx.secondboot.exception.MyRuntimeException; //@ControllerAdvice(annotations=RestController.class) //@ControllerAdvice(basePackages={"com.xxx","com.ooo"}) @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler (RuntimeException. class ) // @ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class}) // @ExceptionHandler//處理所有異常 @ResponseBody //在返回自定義相應類的情況下必須有,這是@ControllerAdvice注解的規定 public MyExceptionResponse exceptionHandler(RuntimeException e, HttpServletResponse response) { MyExceptionResponse resp = new MyExceptionResponse(); resp.setCode( 300 ); resp.setMsg( "exception-Handler" ); // response.setStatus(600); return resp; } } |
說明:
- @ControllerAdvice是controller的一個輔助類,最常用的就是作為全局異常處理的切面類
- @ControllerAdvice可以指定掃描范圍
-
@ControllerAdvice約定了幾種可行的返回值,如果是直接返回model類的話,需要使用@ResponseBody進行json轉換
- 返回String,表示跳到某個view
- 返回modelAndView
- 返回model + @ResponseBody
2、controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package com.xxx.secondboot.web; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api; @Api ( "測試controllerAdvice和全局異常處理" ) @RestController @RequestMapping ( "/advice1" ) public class AdviceController { @RequestMapping (value = "/test1" , method = RequestMethod.GET) public String test1() { throw new RuntimeException( "advice1 - exception1" ); } @RequestMapping (value = "/test2" , method = RequestMethod.GET) public String test2() { throw new RuntimeException( "advice1 - exception2" ); } // @ExceptionHandler(RuntimeException.class) // public MyExceptionResponse exceptionHandler() { // MyExceptionResponse resp = new MyExceptionResponse(); // resp.setCode(300); // resp.setMsg("exception-Handler"); // return resp; // } } |
注意:
- 同一個異常被局部范圍異常處理器和全局范圍異常處理器同時覆蓋,會選擇小范圍的局部范圍處理器
- 同一個異常被小范圍的異常類和大范圍的異常處理器同時覆蓋,會選擇小范圍的異常處理器
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/java-zhao/p/5769018.html