Mr丶冷文

文章 分类 评论
133 13 8620

站点介绍

冷文学习者(KEVINLU98.COM),记录一个北漂小码农的日常业余生活
友链申请地址(直接评论即可): 传送门

SpringBoot自定义错误页面

MR丶冷文 2022-01-08 2897 1条评论 技术学习 javaspringspringboot404500403

首页 / 正文
Freewind主题v1.5版本已发布,下载请移步Freewind 1.5,同时还有主题伴生插件Freewind Markdown,下载请移步 Freewind Markdown,有问题请在留言板,交换友链请直接在友链留言,我创建了一个主题交流群,有兴趣可以加下: 点此加入
报毒我说明一下,是因为我把主题的版权信息做了加密,其中用了eval,杀毒软件认为eval函数是一个危险的操作,这点介意的话请勿下载,我也没有强迫任何人去下载,也没有向大家收取一分钱的主题费用,所以也犯不着因为这些事情来喷我,喜欢就用,不喜欢就不用,就这么简单

发布于2022-10-28

SpringBoot自定义错误页面

问题

我们用springboot开发网站的时候难免会遇到需要自定义错误页面的场景,如403页面、404页面、500页面等,因为一般的服务器程序自带的默认错误页面确实有些丑或错误页面的风格与网站整体风格不挡,比如tomcat的错误页面

解决

准备错误页面

比如我这里准备了三个常见的错误页面

创建Controller

package cn.kevinlu98.cloud.freewindcloud.controller;

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

/**
 * Author: Mr丶冷文
 * Date: 2022-01-08 15:15
 * Email: kevinlu98@qq.com
 * Description:
 */
@Controller
public class ErrorController {

    @GetMapping("/403")
    public String page403() {
        return "error/403";
    }

    @GetMapping("/404")
    public String page404() {
        return "error/404";
    }

    @GetMapping("/500")
    public String page500() {
        return "error/500";
    }
}

将WebServerFactoryCustomizer放入spring容器中

我们将错误页面的路由配置到WebServerFactoryCustomizer里并将其放入spring容器

package cn.kevinlu98.cloud.freewindcloud.config;

import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

/**
 * Author: Mr丶冷文
 * Date: 2022-01-08 15:09
 * Email: kevinlu98@qq.com
 * Description:
 */
@Configuration
public class ErrorPageConfig {


    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        return factory -> {
            factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404"), new ErrorPage(HttpStatus.FORBIDDEN, "/403"), new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500"));
        };
    }
}

效果

我们来看看效果

评论(1)

  1. 常瑞 游客 2022-01-11 17:26 回复

    可以可以

热门文章

最新评论

  • https://titten.xnxxo.cc/

    Whaat yoou wrote was actually very reasonable. But, think on this, what if yoou added a little information? I mean, I don't want to tell you how to run your blog, but what if you added somethkng that grabbed folk's attention? I mean 一点小遗憾 - 冷文学习者 is a little plain. You should loopk att Yahoo's home page and note howw they crete post hesdlines to get vieewers interested. You might add a related video oor a picture or two to grab peopke excited about wat you've written. Just my opinion, it might make your website a little bitt more interesting. xnxx

  • snailwalk

    下载看看

  • https://bonusstats.com/en/bonus-7signs

    I drop a comment whenever I like a article on a site or I have something to valuable to contribute to the discussion. Usually it is a result of the fire displayed in the article I looked at. And on this article Freewind主题音乐播放器(老王重制版) - 冷文学习者. I was actually excited enough to post a comment :-) I actually do have a few questions for you if it's okay. Could it be just me or do some of these responses appear like left by brain dead people? :-P And, if you are posting at other places, I would like to keep up with everything new you have to post. Could you list the complete urls of your communal sites like your linkedin profile, Facebook page or twitter feed? Feel free to surf to my blog post :: https://bonusstats.com/en/bonus-7signs

  • https://Bonusstats.com/en/bonus-7signs

    Thanks for finally talking about >Freewind 1.5 更新 - 冷文学习者

  • 老王

    下载看看

日历

2026年12月

日一二三四五六
  12345
6789101112
13141516171819
20212223242526
2728293031  

文章目录

站点公告
Freewind主题v1.5版本已发布,下载请移步Freewind 1.5,同时还有主题伴生插件Freewind Markdown,下载请移步 Freewind Markdown,有问题请在留言板,交换友链请直接在友链留言,我创建了一个主题交流群,有兴趣可以加下: 点此加入
报毒我说明一下,是因为我把主题的版权信息做了加密,其中用了eval,杀毒软件认为eval函数是一个危险的操作,这点介意的话请勿下载,我也没有强迫任何人去下载,也没有向大家收取一分钱的主题费用,所以也犯不着因为这些事情来喷我,喜欢就用,不喜欢就不用,就这么简单
点击小铃铛关闭