SpringBoot全局异常页面处理学习

首先我们先在控制器中写一个异常,默认情况下我们的SpringBoot异常页面是这个样子的。

示例代码如下:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qinxun
 * @date 2023-06-15
 * @Descripion: 测试
 */
@RestController
public class IndexController {

    @GetMapping("/index")
    public String toIndex() {
        // 500状态码
        int i = 1 / 0;
        return "index";
    }
}

 一、自定义静态异常页面

自定义静态异常页面,我们可以分成两种方式,第一种就是使用HTTP状态码来命名页面,例如404.html,403.html,500html等。另一种就是直接定义一个4xx.html,表示400-499状态码的页面,5xx.html表示状态码500-599的异常页面。

我们在classpath:/static/error/路径下定义相关异常页面

 4xx.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <div>Http状态码400-499的异常页面</div>
</body>
</html>

5xx.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>Http状态码500-599的异常页面</div>
</body>
</html>

启动项目,如果项目抛出HTTP状态码500的错误,就会自动显示5xx.html这个页面,反之抛出状态码400的错误,就会显示4xx.html这个页面。

 二、自定义动态异常页面

 4xx.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>4xx</h1>
<table border="1">
    <tr>
        <td>path</td>
        <td>error</td>
        <td>message</td>
        <td>status</td>
    </tr>
    <tr>
        <td th:text="${path}"></td>
        <td th:text="${error}"></td>
        <td th:text="${message}"></td>
        <td th:text="${status}"></td>
    </tr>
</table>
</body>
</html>

5xx.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>5xx</h1>
<table border="1">
    <tr>
        <td>path</td>
        <td>error</td>
        <td>message</td>
        <td>status</td>
    </tr>
    <tr>
        <td th:text="${path}"></td>
        <td th:text="${error}"></td>
        <td th:text="${message}"></td>
        <td th:text="${status}"></td>
    </tr>
</table>
</body>
</html>

如果动态异常页面和静态异常页面同时存在,默认使用动态页面,完整的错误页面查找方式应该是:

发生500错误->查找动态500.html页面->查找静态500.html页面->查找动态5xx.html页面->查找静态5xx.html页面

页面上使用的path、error、message、status默认为SpringBoot的异常机制返回的。

我们重新访问接口:

 我们可以创建一个自定义类实现异常数据返回处理。

示例代码如下:

import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

/**
 * @author qinxun
 * @date 2023-06-16
 * @Descripion: 自定义异常数据返回
 */
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {
    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
        Map<String, Object> map = super.getErrorAttributes(webRequest, options);
        if ((Integer) map.get("status") == 500) {
            map.put("message", "服务器内部错误!");
        }
        return map;
    }
}

启动项目,我们重新访问接口:

 异常页面显示了我们自定义的异常数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinxun2008081

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值