SpringBoot 統一頁面異常處理

相關博客:SpringBoot 統一異常處理

第一步:創建項目

添加Maven依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 集成thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>nz.net.ultraq.thymeleaf</groupId>
    <artifactId>thymeleaf-layout-dialect</artifactId>
    <version>2.4.1</version>
</dependency>
<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>

application.yml

server:
    port: 80
    servlet:
        context-path: /wpe
spring:
    freemarker:
        enabled: true #是否啓用thymeleaf作爲視圖解析
        cache: false # 緩存配置 開發階段應該配置爲false 因爲經常會改
        charset: UTF-8 # 文件編碼
        #前綴,也就是模板存放的路徑
        prefix: classpath:/templates/
        suffix: .html # 模版後綴名 默認爲ftl
        #編碼格式
        encoding: UTF-8
        #設置不嚴格的html
        mode: HTML
        servlet:
            content-type: text/html

第二步:自定義異常

CustomException.java:

public class CustomException extends RuntimeException {
    // 異常錯誤編碼
    private Integer code;
    // 異常信息
    private String msg;
    private CustomException() {
    }
    public CustomException(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public Integer getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
}

ModelAndViewException.java

public class ModelAndViewException extends RuntimeException {
    // 異常錯誤編碼
    private Integer code;
    // 異常信息
    private String msg;
    private ModelAndViewException() {
    }
    public static ModelAndViewException transfer(CustomException e) {
        return new ModelAndViewException(e.getCode(), e.getMessage());
    }
    private ModelAndViewException(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public Integer getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
}

第三步:自定義異常處理器

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(ModelAndViewException.class)
    public ModelAndView viewExceptionHandler(HttpServletRequest req, ModelAndViewException e) {
        ModelAndView modelAndView = new ModelAndView();
        //將異常信息設置如modelAndView
        modelAndView.addObject("exception", e);
        modelAndView.addObject("url", req.getRequestURL());
        modelAndView.setViewName("error");
        //返回ModelAndView
        return modelAndView;
    }
}

第四步:自定義註解

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface ModelAndView {
}

第五步:創建Controller

@Controller
@RequestMapping("/mave")
@Slf4j
public class ModelAndViewExceptionController {
    /**
     * 模擬系統異常
     */
    @ModelAndView
    @GetMapping("/fun1")
    public String fun1() {
        try {
            Class.forName("com.mysql.jdbc.xxxx.Driver");
        } catch (ClassNotFoundException e) {
            throw new CustomException(400,"在XXX業務,ff1()方法內,出現ClassNotFoundException");
        }
        return "index";
    }
}

第六步:在resources/templates目錄下創建error.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
    <head lang="en">
        <meta charset="UTF-8"/>
        <title>錯誤頁面</title>
    </head>
    <body>
        <h1>error</h1>

        <div th:text="${exception}"></div>
        <div th:text="${url}"></div>
    </body>
</html>

第七步:啓動項目,結果:

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章