温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot怎么自定义404、500错误提示页面

发布时间:2021-11-29 09:17:23 来源:亿速云 阅读:537 作者:iii 栏目:开发技术

本篇内容介绍了“springboot怎么自定义404、500错误提示页面”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

    springboot 默认的异常处理机制

    springboot 默认已经提供了一套处理异常的机制。一旦程序中出现了异常 springboot 会向 /errorurl 发送请求。在 springboot 中提供了一个名为 BasicErrorController 的类来处理 /error 请求,然后跳转到默认显示异常的页面来展示异常信息

    使用模板引擎

    在使用 thymeleaf 等模板引擎时,springboot 会自动到 src/main/resources/templates/error/,文件夹下寻找 404.html、500.html 的错误提示页面

    错误提示页面的命名规则就是:错误码.html,如 404404.html500500.html

    使用示例

    创建 springboot 项目如下

    404、500 错误提示页面结构如下

    springboot怎么自定义404、500错误提示页面

    application.properties 项目配置文件

    server.port=8080
    
    #它的默认值就是classpath:/templates/,源码在ThymeleafProperties类中
    spring.mvc.view.prefix=classpath:/templates/
    #它的默认值就是.html,源码在ThymeleafProperties类中
    spring.mvc.view.suffix=.html
    spring.thymeleaf.cache=false

    404 页面内容如下

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>404</title>
        <link rel="shortcut icon" type="image/x-icon" th:href="@{/img/favicon.ico}" rel="external nofollow"  rel="external nofollow" />
        <link rel="stylesheet" type="text/css" th:href="@{/css/404.css}" rel="external nofollow" />
    </head>
    <body>
    	<div id="banner" ></div>
    </body>
    </html>

    500 页面内容如下

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>500</title>
        <link rel="shortcut icon" type="image/x-icon" th:href="@{/img/favicon.ico}" rel="external nofollow"  rel="external nofollow" />
        <link rel="stylesheet" type="text/css" th:href="@{/css/500.css}" rel="external nofollow" />
    </head>
    <body>
        <div id="banner" ></div>
    </body>
    </html>

    controller 如下

    @Controller
    public class PageController {
    
        // 跳转到登录页
        @GetMapping(path = "/toLogin")
        public String toLogin() {
            int code = 1/0;
            return "login";
        }
    }

    404.html 页面测试

    访问不存在的接口:http://localhost:8080/aaaa,结果如下

    springboot怎么自定义404、500错误提示页面

    500.html 页面测试

    访问已存在的接口:http://localhost:8080/toLogin,结果如下

    springboot怎么自定义404、500错误提示页面

    没有使用模板引擎

    如果没有使用 thymeleaf 等模板引擎时,springboot 会到静态资源文件夹寻找 404.htm、500.html的错误提示页面,命名同上。springboot 中默认的静态资源路径有 4 个,分别是

    • classpath:/METAINF/resources/

    • classpath:/resources/

    • classpath:/static/

    • classpath:/public/

    优先级顺序为:META-INF/resources > resources > static > public,以上 4 种路径创建 error 文件夹,再创建 404、500 错误提示页面如下

    springboot怎么自定义404、500错误提示页面

    不用写额外的映射器,就能直接请求到

    “springboot怎么自定义404、500错误提示页面”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI