千家信息网

springboot ErrorPageFilter怎么应用

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,本篇内容主要讲解"springboot ErrorPageFilter怎么应用",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"springboot Erro
千家信息网最后更新 2026年11月02日springboot ErrorPageFilter怎么应用

本篇内容主要讲解"springboot ErrorPageFilter怎么应用",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"springboot ErrorPageFilter怎么应用"吧!

ErrorPageFilter的实际应用

Spring框架错误页过滤器

springboot提供了一个ErrorPageFilter,用来处理当程序发生错误时如何展现错误,话不多说请看代码

private void doFilter(HttpServletRequest request, HttpServletResponse response,            FilterChain chain) throws IOException, ServletException {    ErrorWrapperResponse wrapped = new ErrorWrapperResponse(response);    try {        chain.doFilter(request, wrapped);        if (wrapped.hasErrorToSend()) {            // 重点关注此方法            handleErrorStatus(request, response, wrapped.getStatus(),                    wrapped.getMessage());            response.flushBuffer();        }        else if (!request.isAsyncStarted() && !response.isCommitted()) {            response.flushBuffer();        }    }    catch (Throwable ex) {        Throwable exceptionToHandle = ex;        if (ex instanceof NestedServletException) {            exceptionToHandle = ((NestedServletException) ex).getRootCause();        }        handleException(request, response, wrapped, exceptionToHandle);        response.flushBuffer();    }}
private void handleErrorStatus(HttpServletRequest request,            HttpServletResponse response, int status, String message)                    throws ServletException, IOException {    if (response.isCommitted()) {        handleCommittedResponse(request, null);        return;    }    // 获取错误页,来关注下这个属性this.statuses,就是一个map,而错误页就是从这属性中获取,那此属性的内容是什么时候添加进去的呢    String errorPath = getErrorPath(this.statuses, status);    if (errorPath == null) {        response.sendError(status, message);        return;    }    response.setStatus(status);    setErrorAttributes(request, status, message);    // 拿到错误页地址后,通过服务器重定向的方式跳转到错误页面    request.getRequestDispatcher(errorPath).forward(request, response);}

ErrorPageFilter implements Filter, ErrorPageRegistry,此类实现了ErrorPageRegistry接口,接口内方法如下,我们可以看到这个入参errorPages便是错误页集合,然后把所有错误页put到statuses属性内,但是此方法入参从何而来呢?

@Overridepublic void addErrorPages(ErrorPage... errorPages) {    for (ErrorPage errorPage : errorPages) {        if (errorPage.isGlobal()) {            this.global = errorPage.getPath();        }        else if (errorPage.getStatus() != null) {            this.statuses.put(errorPage.getStatus().value(), errorPage.getPath());        }        else {            this.exceptions.put(errorPage.getException(), errorPage.getPath());        }    }}

通过源码分析,发现此接口,只要实现此接口并生成bean交给spring,便可以往ErrorPageRegistry添加你自己的错误页了。

public interface ErrorPageRegistrar {    /**     * Register pages as required with the given registry.     * @param registry the error page registry     */    void registerErrorPages(ErrorPageRegistry registry);}

看个例子吧,这样就可以了,是不是很简单。

@Componentpublic class MyErrorPage implements ErrorPageRegistrar {    @Override    public void registerErrorPages(ErrorPageRegistry registry) {        ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/WEB-INF/errorpage/404.html");        ErrorPage error405Page = new ErrorPage(HttpStatus.METHOD_NOT_ALLOWED, "/WEB-INF/errorpage/405.html");        ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/WEB-INF/errorpage/500.html");        registry.addErrorPages(error404Page, error405Page, error500Page);    }}

springboot项目出现ErrorPageFilter异常

今天用springboot(2.2.12.RELEASE)+beetl模板的时候,由于某个模板找不到,

系统一直出现报错日子

[http-nio-8080-exec-1] ERROR o.s.b.w.s.s.ErrorPageFilter - [handleCommittedResponse,219] - Cannot forward to error page for request [/yuantuannews/list/index_1.html] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false

看了网上的一些解决方法,大体上都是重写ErrorPageFilter,然后在FilterRegistrationBean中设置 filterRegistrationBean.setEnabled(false);

代码如下

    @Bean    public ErrorPageFilter errorPageFilter() {        return new ErrorPageFilter();    }     @Bean    public FilterRegistrationBean disableSpringBootErrorFilter(ErrorPageFilter filter) {        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();        filterRegistrationBean.setFilter(filter);        filterRegistrationBean.setEnabled(false);        return filterRegistrationBean;    }

按照这个方法,我做了多次尝试,系统直接报错说errorPageFilter冲突了,原来是

ErrorPageFilterConfiguration.java中已经定义了这么一个bean:

//// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)//package org.springframework.boot.web.servlet.support;import javax.servlet.DispatcherType;import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration(    proxyBeanMethods = false)class ErrorPageFilterConfiguration {    ErrorPageFilterConfiguration() {    }    @Bean    ErrorPageFilter errorPageFilter() {        return new ErrorPageFilter();    }    @Bean    FilterRegistrationBean errorPageFilterRegistration(ErrorPageFilter filter) {        FilterRegistrationBean registration = new FilterRegistrationBean(filter, new ServletRegistrationBean[0]);        registration.setOrder(filter.getOrder());        registration.setDispatcherTypes(DispatcherType.REQUEST, new DispatcherType[]{DispatcherType.ASYNC});        return registration;    }}

最后我的解决方式是在启动类中设置:

@SpringBootApplicationpublic class App extends SpringBootServletInitializer {public App() {    super();    //下面设置为false    setRegisterErrorPageFilter(false); }@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {    return application.sources(App.class);}public static void main(String[] args) {    SpringApplication.run(App.class, args);}

到此,相信大家对"springboot ErrorPageFilter怎么应用"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

错误 应用 属性 接口 方法 内容 代码 实际 方式 时候 模板 此方法 系统 中设 学习 实用 更深 例子 兴趣 地址 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 为想互联网科技怎么样 鸠鸠互联网科技 科技互联网公司装修 奇士达互联网科技 互联网 视角下的科技创新 深圳卡尔迅互联网科技有限公司怎么样 恒银科技与互联网金融 央行互联网金融科技公司 团飞科技与伯堂互联网服务平台 互联网还是高科技吗 赶超互联网科技 微信多开 移动互联网科技公司 用友工业互联网科技发展 徐州市互联网科技有限公司 锡山区互联网智能科技产品供应 小米互联网科技园入驻 郑州欣桑互联网科技有限公司 互联网科技平台产品运营 互联网公司属于科技吗 恒生科技互联网指数十大成分股 国智互联网科技 互联网科技巨头监管 北京互联网科技公司 互联网科技革命 天津腾达互联网科技有限公司 河南住房之家互联网科技 珠海东科互联网科技有限公司 陕西九四连强互联网科技有限公司 恒生科技指数 中概互联网 互联网 科技节活动 微视频大赛
0