SpringBoot從入門到精通-第10章 異常處理
一、異常簡介
傳統的Java程序都是由try-catch語句捕捉異常,而Spring Boot項目采用了全局異常類的概念------所有方法均將異常拋出,并且專門安排一個類統一攔截并處理這些異常。這樣做的好處是可以把異常處理的代碼單獨存儲在一個全局異常處理類中。如果未來需要修改異常處理方案,就可以直接在這個全局異常處理類中進行修改。
二、攔截特定異常
為了攔截異常,Spring Boot提供了兩個注解,即@ControllerAdvice和@ExceptionHandler()注解。
- 其中@ControllerAdvice注解用于標注類,把被@ControllerAdvice注解標注的類稱為全局異常處理類;
- @ExceptionHandler()注解用于標注方法,把被@ExceptionHandler()注解標注的方法用于處理特定異常。
- 使用@ControllerAdvice注解和@ExceptionHandler()注解攔截特定異常的語法如下:
@ControllerAdvice
public class TestContro { @ExceptionHandler(被攔截的異常類)public String exce(){}
}
2.1、先創建一個簡單的springboot項目
編寫控制器類TestContro:
package com.example._2025614spring_exception.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestContro {@RequestMapping("/exception")public String exce(){System.out.println("exce fun");return "exce_fun";}
}
2.2、準備創建正常數組訪問的代碼
package com.example._2025614spring_exception.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;@RestController
public class TestContro {@RequestMapping("/exception")public String exce(){ArrayList<String> list = new ArrayList<>();list.add("aa");list.add("bb");list.add("cc");System.out.println("list[2]"+list.get(2));System.out.println("exce fun");return "exce_fun";}
}
啟動后訪問,控制臺輸出:
2.3、準備創建讓數組訪問超出邊界的代碼
package com.example._2025614spring_exception.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;@RestController
public class TestContro {@RequestMapping("/exception")public String exce(){ArrayList<String> list = new ArrayList<>();list.add("aa");list.add("bb");list.add("cc");System.out.println("list[2]"+list.get(2));System.out.println("list[3]"+list.get(3));System.out.println("exce fun");return "exce_fun";}
}
2.4、啟動正常,訪問http://127.0.0.1:8080/exception
后臺日志:
2.5、添加異常攔截,這時候啟動程序,訪問時沒有報錯打印出來,說明異常被攔截了
package com.example._2025614spring_exception.controller;import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
@ControllerAdvice
//@RestController
public class TestContro {@RequestMapping("/exception")@ExceptionHandler(ArrayIndexOutOfBoundsException.class)public String exce(){ArrayList<String> list = new ArrayList<>();list.add("aa");list.add("bb");list.add("cc");System.out.println("list[2]"+list.get(2));System.out.println("list[3]"+list.get(3));System.out.println("exce fun");return "exce_fun";}}
三、打印異常日志
添加異常信息打印
package com.example._2025614spring_exception.controller;import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.lang.reflect.Array;
import java.util.ArrayList;
@ControllerAdvice
@RestController
public class TestContro {@RequestMapping("/exception")@ExceptionHandler(ArrayIndexOutOfBoundsException.class)public String exce(ArrayIndexOutOfBoundsException e){System.out.println("數組下標越界攔截到了,報錯信息為:"+e);
// ArrayList<String> list = new ArrayList<>();
// list.add("aa");
// list.add("bb");
// list.add("cc");
// System.out.println("list[2]"+list.get(2));
// System.out.println("list[3]"+list.get(3));int[] arr = new int[]{1,2,3,4};System.out.println("arr[1]"+arr[1]);System.out.println("exce fun");return "exce_fun";}
}
訪問:
四、縮小攔截異常的范圍
4.1、攔截由某個或多個包觸發的異常
@ControllerAdvice({"包名1", "包名2"})
@ControllerAdvice("包名")
4.2、攔截由某個或多個注解標注的類觸發的異常
@ControllerAdvice(annotations={注解1.class, 注解2.class})
@ControllerAdvice(annotations=注解.class)
五、攔截自定義異常
- 創建自定義異常類,這個類必須集成RuntimeException運行時異常類,并重寫父類的構造方法。
- 創建全局異常類,用于攔截自定義的異常。
- 創建控制器類,指定自定義異常的觸發條件。