SpringBoot全局异常处理(三十)上

一. 为什么要实现异常信息自定义展示

在 Springboot 项目开发中,包括以前的 SSM 框架开发中,我们常常会碰到 404 500 相应的错误信息.


如访问一个除以 0 的功能 (500) 错误

@RequestMapping("/div")
    @ResponseBody
    public String div(){
        int result=10/0;
        return "相除后的结果是:"+result;
    }


SpringBoot全局异常处理(三十)上


或者访问一个不存在的页面 404 错误

SpringBoot全局异常处理(三十)上

这两个页面都是 SpringBoot 默认提供的.


用户在使用的过程中,如果没有相应的开发或者网络经验,对这些提示信息是很讨厌的。


所以常常进行一些有趣的设计,来减轻项目运行错误导致的不良感受。


如: (以下图片来源于网络)


SpringBoot全局异常处理(三十)上

SpringBoot全局异常处理(三十)上



我们可以自定义错误页面.


二. 自定义错误页面

二.一 Tomcat 服务器配置错误页面

在以前的 Tomcat 和 JSP 时代 ,我们可以这样配置错误页面.


二.一.一 自定义 404.jsp 和 505.jsp 页面

404.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true"%>
<% response.setStatus(HttpServletResponse.SC_OK);%>
404,地址错误


500.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true"%>
<% response.setStatus(HttpServletResponse.SC_OK);%>
500, 服务器错误,联系管理员


二.一.二 web.xml 配置信息

在 web.xml 配置中,进行配置

<!--错误状态码指定-->  
<error-page>
  <error-code>404</error-code>
  <location>/WEB-INF/jsp/404.jsp</location>
    </error-page>
   <!--异常类型指定,也可以细化一下-->
    <error-page>
  <exception-type>java.lang.Throwable</exception-type>
  <location>/WEB-INF/jsp/500.jsp</location>
    </error-page>
   <!--错误状态码指定-->
    <error-page>
  <error-code>500</error-code>
  <location>/WEB-INF/jsp/500.jsp</location>
    </error-page>

二. 二SpringBoot 配置错误页面

根据上面的提示,我们知道,需要在 /error 目录下进行配置相关的页面信息.


配置错误页面,包括两种,一种是静态的异常页面,一种是动态的异常页面.


二.二.一 静态异常页面

在 static 目录下 创建 error 目录, 里面放置 404.html 和 500.html 页面


SpringBoot全局异常处理(三十)上


404.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>404异常</title>
</head>
<body>
    404异常
</body>
</html>


500.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>500 异常信息</title>
</head>
<body>
    500异常
</body>
</html>


重新运行服务器,并且访问


在访问 div (有异常的方法)

SpringBoot全局异常处理(三十)上


在访问 404 错误页面时

SpringBoot全局异常处理(三十)上


就变成了我们自定义的异常页面了.


除了指定 404 ,500 这样确切的错误状态码外,也可以使用 4xx, 5xx 这样的来统一进行接收.


二.二.二 动态错误页面

有时候常常需要将错误的信息展示出来,便于开发人员进行处理.


可以使用动态错误页面,通常放置在 templates 目录下


在 templates 目录下,创建动态错误页面

SpringBoot全局异常处理(三十)上



404.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>404状态码展示错误</title>
</head>
<body>
<h1>404</h1>
<table border="1">
    <tr>
        <td>path</td>
        <td th:text="${path}"></td>
    </tr>
    <tr>
        <td>error</td>
        <td th:text="${error}"></td>
    </tr>
    <tr>
        <td>message</td>
        <td th:text="${message}"></td>
    </tr>
    <tr>
        <td>timestamp</td>
        <td th:text="${timestamp}"></td>
    </tr>
    <tr>
        <td>status</td>
        <td th:text="${status}"></td>
    </tr>
</table>
</body>
</html>


500.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>5xx状态码展示错误</title>
</head>
<body>
<h1>5xx</h1>
<table border="1">
    <tr>
        <td>path</td>
        <td th:text="${path}"></td>
    </tr>
    <tr>
        <td>error</td>
        <td th:text="${error}"></td>
    </tr>
    <tr>
        <td>message</td>
        <td th:text="${message}"></td>
    </tr>
    <tr>
        <td>timestamp</td>
        <td th:text="${timestamp}"></td>
    </tr>
    <tr>
        <td>status</td>
        <td th:text="${status}"></td>
    </tr>
</table>
</body>
</html>


这五个属性信息, path, error,message,timestamp,status


是 由: org.springframework.boot.web.reactive.error.DefaultErrorAttributes 进行定义的


SpringBoot全局异常处理(三十)上


这个时候,再进行访问


SpringBoot全局异常处理(三十)上


SpringBoot全局异常处理(三十)上


会展示出具体的信息.


发现,动态的错误页面是生效的。


如果动态页面和静态页面同时定义了异常处理页面,


例如 classpath:/static/error/404.html 和 classpath:/templates/error/404.html 同时存在时,


默认使用动态页面。


完整的错误页面查找方式应该是这样:


发生了 404错误–>查找动态 404.html 页面–>查找静态 404.html –> 查找动态 4xx.html–>查找静态 4xx.html。


500 也一样.


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


上一篇:webpack4+react多页面架构


下一篇:SpringBoot自定义日志Starter(二十五)上