吴裕雄天生自然Spring BootSpring Boot的异常统一处理

自定义error页面

    在Spring Boot Web应用的src/main/resources/templates目录下添加error.html页面,访问发生错误或异常时,Spring Boot将自动找到该页面作为错误页面。Spring Boot为错误页面提供了以下属性:
timestamp:错误发生时间;
status:HTTP状态吗;
error:错误原因;
exception:异常的类名;
message:异常消息(如果这个错误是由异常引起的);
errors:BindingResult异常里的各种错误(如果这个错误是由异常引起的);
trace:异常跟踪信息(如果这个错误是由异常引起的);
path:错误发生时请求的URL路径。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>error</title>
<link rel="stylesheet" th:href="@{css/bootstrap.min.css}" />
<!-- 默认访问 src/main/resources/static下的css文件夹-->
<link rel="stylesheet" th:href="@{css/bootstrap-theme.min.css}" />
</head>
<body>
  <div class="panel-l container clearfix">
        <div class="error">
            <p class="title"><span class="code" th:text="${status}"></span>非常抱歉,没有找到您要查看的页面</p>
            <div class="common-hint-word">
                <div th:text="${#dates.format(timestamp,'yyyy-MM-dd HH:mm:ss')}"></div>
                <div th:text="${message}"></div>
                <div th:text="${error}"></div>
            </div>
        </div>
    </div>
</body>
</html>
1.创建基于Thymeleaf模板引擎的Spring Boot Web应用ch5_3

2.设置Web应用ch5_3的上下文路径

3.创建自定义异常类MyException

4.创建控制器类TestHandleExceptionController

5.整理脚本样式静态文件

6.View视图页面

7.运行
创建自定义异常类MyException

package com.ch.ch5_3.exception;

public class MyException extends Exception {
    private static final long serialVersionUID = 1L;

    public MyException() {
        super();
    }

    public MyException(String message) {
        super(message);
    }
}
创建名为com.ch.ch5_3.controller的包,并在该包中创建名为TestHandleExceptionController的控制器类。在该控制器类中,有4个请求处理方法,一个是导航到index.html,另外3个分别抛出不同的异常(并没有处理异常)。
@RequestMapping("/")
    public String index() {
        return "index";
    }
    @RequestMapping("/db")  
    public void db() throws SQLException { 
        throw new SQLException("数据库异常");
    }  
    @RequestMapping("/my")  
    public void my() throws MyException {  
        throw new MyException("自定义异常");
    }
    @RequestMapping("/no")  
    public void no() throws Exception {  
        throw new Exception("未知异常");
    } 
package com.ch.ch5_3.controller;

import java.sql.SQLException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ch.ch5_3.exception.MyException;

@Controller
public class TestHandleExceptionController {
    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @RequestMapping("/db")
    public void db() throws SQLException {
        throw new SQLException("数据库异常");
    }

    @RequestMapping("/my")
    public void my() throws MyException {
        throw new MyException("自定义异常");
    }

    @RequestMapping("/no")
    public void no() throws Exception {
        throw new Exception("未知异常");
    }
}
Tymeleaf模板默认将视图页面放在src/main/resources/templates目录下。因此,我们在src/main/resources/templates目录下新建html页面文件index.html和error.html。
在index.html页面中,有4个超链接请求,3个请求在控制器中有对应处理,另一个请求是404错误。
package com.ch.ch5_3;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Ch53Application {
    public static void main(String[] args) {
        SpringApplication.run(Ch53Application.class, args);
    }
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>index</title>
<link rel="stylesheet" th:href="@{css/bootstrap.min.css}" />
<!-- 默认访问 src/main/resources/static下的css文件夹-->
<link rel="stylesheet" th:href="@{css/bootstrap-theme.min.css}" />
</head>
<body>
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">异常处理示例</h3>
        </div>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-md-4 col-sm-6">
                <a th:href="@{db}">处理数据库异常</a><br>
                <a th:href="@{my}">处理自定义异常</a><br>
                <a th:href="@{no}">处理未知错误</a>
                <hr>
                <a th:href="@{nofound}">404错误</a>
            </div>
        </div>
    </div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>myError</title>
</head>
<body>
    自定义异常。
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    未知异常。
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>sql</title>
</head>
<body>
    SQL异常。
</body>
</html>

 

 

 

原文地址:https://www.cnblogs.com/tszr/p/15333909.html