SpringBoot 實戰 (十四) | 統一處理異常

微信公衆號:一個優秀的廢人 若有問題或建議,請後臺留言,我會盡力解決你的問題。前端

前言

如題,今天介紹 SpringBoot 是如何統一處理全局異常的。SpringBoot 中的全局異常處理主要起做用的兩個註解是 @ControllerAdvice@ExceptionHandler ,其中 @ControllerAdvice 是組件註解,添加了這個註解的類可以攔截 Controller 的請求,而 ExceptionHandler 註解能夠設置全局處理控制裏的異常類型來攔截要處理的異常。 好比:@ExceptionHandler(value = NullPointException.class) 。java

準備工做

  • SpringBoot 2.1.3
  • IDEA
  • JDK 8

依賴配置

<dependencies>
        <!-- JPA 依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- web 依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- mysql 鏈接類 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- lombok 依賴 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- 單元測試依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
複製代碼

配置文件

spring:
  # 數據庫相關
 datasource:
 driver-class-name: com.mysql.jdbc.Driver
 url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=true
 username: root
 password: 123456

 jpa:
 hibernate:
 ddl-auto: update   #ddl-auto:設爲 create 表示每次都從新建表
 show-sql: true
複製代碼

返回的消息類

public class Message<T> implements Serializable {

    /** * 狀態碼 */
    private Integer code;

    /** * 返回信息 */
    private String message;

    /** * 返回的數據類 */
    private T data;

    /** * 時間 */
    private Long time;

    // getter、setter 以及 構造方法略。。。
}
複製代碼

工具類

用於處理返回的數據以及信息類,代碼註釋很詳細不說了。mysql

public class MessageUtil {

    /** * 成功並返回數據實體類 * @param o * @param <E> * @return */
    public static <E>Message<E> ok(E o){
        return new Message<>(200, "success", o, new Date().getTime());
    }

    /** * 成功,但無數據實體類返回 * @return */
    public static <E>Message<E> ok(){
        return new Message<>(200, "success", null, new Date().getTime());
    }

    /** * 失敗,有自定義異常返回 * @param code * @param msg * @return */
    public static <E>Message<E> error(Integer code,String msg){
        return new Message<>(code, msg, null, new Date().getTime());
    }
}
複製代碼

自定義異常

經過繼承 RuntimeException ,聲明 code 用於定義不一樣類型的自定義異常。主要是用於異常攔截出獲取 code 並將 code 設置到消息類中返回。git

public class CustomException extends RuntimeException{

    /** * 狀態碼 */
    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public CustomException(Integer code, String message){

        super(message);
        this.code = code;

    }
}
複製代碼

異常攔截類

經過加入 @RestControllerAdvice 來聲明該類可攔截 Controller 請求,同時在 handle方法加入 @ExceptionHandler 並在該註解中指定要攔截的異常類。github

@RestControllerAdvice // 控制器加強處理(返回 JSON 格式數據),添加了這個註解的類能被 classpath 掃描自動發現
public class ExceptionHandle {

    @ExceptionHandler(value = Exception.class) // 捕獲 Controller 中拋出的指定類型的異常,也能夠指定其餘異常
    public <E>Message<E> handler(Exception exception){

        if (exception instanceof CustomException){
            CustomException customException = (CustomException) exception;
            return MessageUtil.error(customException.getCode(), customException.getMessage());
        } else {
            return MessageUtil.error(120, "異常信息:" + exception.getMessage());
        }
    }
}
複製代碼

這裏只對自定義異常以及未知異常進行處理,若是你在某方法中明確知道可能會拋出某個異常,能夠加多一個特定的處理。好比說你明確知道該方法可能拋出 NullPointException 能夠追加 NullPointException 的處理:web

if (exception instanceof CustomException){
     CustomException customException = (CustomException) exception;
     return MessageUtil.error(customException.getCode(), customException.getMessage());
} else if (exception instanceof NullPointException ){
     return MessageUtil.error(500, "空指針異常信!");
} else {
     return MessageUtil.error(120, "異常信息:" + exception.getMessage());
}
複製代碼

controller 層

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/{id}")
    public Message<Student> findStudentById(@PathVariable("id") Integer id){

        if (id < 0){
            //測試自定義錯誤
            throw new CustomException(110, "參數不能是負數!");

        } else if (id == 0){
            //硬編碼,爲了測試
            Integer i = 1/id;
            return null;
        } else {
            Student student = studentService.findStudentById(id);
            return MessageUtil.ok(student);
        }
    }
}
複製代碼

完整代碼

https://github.com/turoDog/Demo/tree/master/springboot_exception_demo算法

若是以爲對你有幫助,請給個 Star 再走唄,很是感謝。spring

Postman 測試

訪問 http://localhost:8080/student/5 測試正常返回數據結果。sql

返回正常結果

訪問 http://localhost:8080/student/0 測試未知異常的結果。數據庫

測試未知異常的狀況

訪問 http://localhost:8080/student/-11 測試自定義異常的結果。

測試自定義異常狀況

後語

若是本文對你哪怕有一丁點幫助,請幫忙點好看。你的好看是我堅持寫做的動力。

另外,關注以後在發送 1024 可領取免費學習資料。資料內容詳情請看這篇舊文:Python、C++、Java、Linux、Go、前端、算法資料分享

一個優秀的廢人
相關文章
相關標籤/搜索