天天看點

七、SpringBoot全局異常捕獲

​ 在項目使用全局異常捕獲,可以簡化我們的代碼,做到異常統一管理傳回的效果。本節我們來學學springboot項目中是如何來全局捕獲異常。

1、引入Maven依賴

​ 由于本節的實戰,我們會學習一下JSON封裝傳回和視圖傳回這兩種方式。視圖傳回使用FreeMarker來演練。

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <!-- FreeMarker依賴包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
    </dependencies>
           

2、編寫全局異常捕獲類

我們可以在啟動類同級包以及子級包下新增全局異常捕獲類,并在該類上使用注解**@ControllerAdvice**,它是一個Controller的切面。

/**
 * @author 小吉
 * @description springboot全局異常捕獲
 * @date 2020/5/29
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Map handler500(Exception e){
        e.printStackTrace();
        Map map = new HashMap();
        map.put("status",false);
        map.put("message","服務異常!");
        return map;
    }
}
           

3、編寫控制器

我們嘗試在控制器中加上報錯的代碼,用來測試一下全局異常的捕獲。

/**
 * @author 小吉
 * @description
 * @date 2020/5/29
 */
@RestController
public class IndexController {

    @RequestMapping("index")
    @ResponseBody
    public String index(){
        int i = 1 / 0;
        return "SpringBoot全局異常捕獲";
    }
}
           

啟動類

/**
 * @author 小吉
 * @description springboot啟動類
 * @date 2020/5/29
 */
@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }
}
           

啟動應用後在控制台上可以看到springboot已經加載好異常處理。

七、SpringBoot全局異常捕獲
七、SpringBoot全局異常捕獲

當看到浏覽器上傳回我們的報錯資訊,證明全局異常捕獲生效。

以上就是全局異常捕獲傳回json資訊。接下來我來介紹視圖傳回方式。

4、視圖傳回

我們可以借助ModelAndView來實作視圖傳回。

/**
 * @author 小吉
 * @description springboot全局異常捕獲
 * @date 2020/5/29
 */
@ControllerAdvice
public class GlobalExceptionHandler {

//    @ExceptionHandler(Exception.class)
//    @ResponseBody
//    public Map handler500(Exception e){
//        e.printStackTrace();
//        Map map = new HashMap();
//        map.put("status",false);
//        map.put("message","服務異常!");
//        return map;
//    }

    @ExceptionHandler(Exception.class)
    public ModelAndView handler500(Exception e){
        e.printStackTrace();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message","服務異常!");
        modelAndView.setViewName("500");
        return modelAndView;
    }
}
           

在src/main/resources下建立templates目錄,建立500.ftl

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
           
七、SpringBoot全局異常捕獲