最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot?ErrorPageFilter的實際應用詳解

 更新時間:2022年01月27日 14:50:56   作者:零零落落。  
這篇文章主要介紹了springboot?ErrorPageFilter的實際應用詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

ErrorPageFilter的實際應用

Spring框架錯誤頁過濾器

springboot提供了一個ErrorPageFilter,用來處理當程序發(fā)生錯誤時如何展現(xiàn)錯誤,話不多說請看代碼

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,此類實現(xiàn)了ErrorPageRegistry接口,接口內方法如下,我們可以看到這個入參errorPages便是錯誤頁集合,然后把所有錯誤頁put到statuses屬性內,但是此方法入參從何而來呢?

@Override
public 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());
? ? ? ? }
? ? }
}

通過源碼分析,發(fā)現(xiàn)此接口,只要實現(xiàn)此接口并生成bean交給spring,便可以往ErrorPageRegistry添加你自己的錯誤頁了。

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

看個例子吧,這樣就可以了,是不是很簡單。

@Component
public 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項目出現(xiàn)ErrorPageFilter異常

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

系統(tǒng)一直出現(xiàn)報錯日子

[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;
? ? }

按照這個方法,我做了多次嘗試,系統(tǒng)直接報錯說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<ErrorPageFilter> errorPageFilterRegistration(ErrorPageFilter filter) {
        FilterRegistrationBean<ErrorPageFilter> registration = new FilterRegistrationBean(filter, new ServletRegistrationBean[0]);
        registration.setOrder(filter.getOrder());
        registration.setDispatcherTypes(DispatcherType.REQUEST, new DispatcherType[]{DispatcherType.ASYNC});
        return registration;
    }
}

最后我的解決方式是在啟動類中設置:

@SpringBootApplication
public class App extends SpringBootServletInitializer {
public App() {
? ? super();
? ? //下面設置為false
? ? setRegisterErrorPageFilter(false);?
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
? ? return application.sources(App.class);
}
public static void main(String[] args) {
? ? SpringApplication.run(App.class, args);
}

問題解決。 

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • SpringBoot配置文件中數(shù)據(jù)庫密碼加密兩種方案(推薦)

    SpringBoot配置文件中數(shù)據(jù)庫密碼加密兩種方案(推薦)

    SpringBoot項目經常將連接數(shù)據(jù)庫的密碼明文放在配置文件里,安全性就比較低一些,尤其在一些企業(yè)對安全性要求很高,因此我們就考慮如何對密碼進行加密,文中給大家介紹加密的兩種方式,感興趣的朋友一起看看吧
    2019-10-10
  • Java線程休眠之sleep方法詳解

    Java線程休眠之sleep方法詳解

    這篇文章主要介紹了Java線程休眠之sleep方法詳解,Thread?類中有一個靜態(tài)方法的sleep方法,當該線程調用sleep方法后,就會暫時讓CPU的調度權,但是監(jiān)視器資源比如鎖并不會釋放出去,需要的朋友可以參考下
    2024-01-01
  • Mybatis分頁插件使用方法詳解

    Mybatis分頁插件使用方法詳解

    這篇文章主要為大家詳細介紹了Mybatis分頁插件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • 最常用的1000個Java類(附代碼示例)

    最常用的1000個Java類(附代碼示例)

    這篇文章主要介紹了最常用的1000個Java類(附代碼示例),需要的朋友可以參考下
    2015-04-04
  • 詳解Spring boot上配置與使用mybatis plus

    詳解Spring boot上配置與使用mybatis plus

    這篇文章主要介紹了詳解Spring boot上配置與使用mybatis plus,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Java設計模式之觀察者模式

    Java設計模式之觀察者模式

    這篇文章主要介紹了Java設計模式之觀察者模式,觀察者模式,是一種行為性模型,又叫發(fā)布-訂閱模式,他定義對象之間一種一對多的依賴關系,使得當一個對象改變狀態(tài),則所有依賴于它的對象都會得到通知并自動更新,需要的朋友可以參考下
    2023-11-11
  • springboot2.3 整合mybatis-plus 高級功能及用法詳解

    springboot2.3 整合mybatis-plus 高級功能及用法詳解

    這篇文章主要介紹了springboot2.3 整合mybatis-plus 高級功能,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Java Map 按照Value排序的實現(xiàn)方法

    Java Map 按照Value排序的實現(xiàn)方法

    Map是鍵值對的集合接口,它的實現(xiàn)類主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等。這篇文章主要介紹了Java Map 按照Value排序的實現(xiàn)方法,需要的朋友可以參考下
    2016-08-08
  • java實現(xiàn)cassandra高級操作之分頁實例(有項目具體需求)

    java實現(xiàn)cassandra高級操作之分頁實例(有項目具體需求)

    這篇文章主要介紹了java實現(xiàn)cassandra高級操作之分頁實例(有項目具體需求),具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • 一文徹底弄懂零拷貝原理以及java實現(xiàn)

    一文徹底弄懂零拷貝原理以及java實現(xiàn)

    零拷貝(英語: Zero-copy) 技術是指計算機執(zhí)行操作時,CPU不需要先將數(shù)據(jù)從某處內存復制到另一個特定區(qū)域,下面這篇文章主要給大家介紹了關于零拷貝原理以及java實現(xiàn)的相關資料,需要的朋友可以參考下
    2021-08-08

最新評論

黔江区| 诸城市| 新安县| 宜州市| 南溪县| 永吉县| 玉林市| 四会市| 新竹县| 阿尔山市| 麻城市| 砚山县| 龙口市| 红原县| 旬邑县| 原平市| 和顺县| 宁城县| 昔阳县| 庆城县| 鹤山市| 泗阳县| 安康市| 涪陵区| 靖宇县| 遵化市| 镇安县| 安乡县| 陆良县| 舒城县| 湄潭县| 安宁市| 出国| 梅河口市| 陈巴尔虎旗| 昌邑市| 洛阳市| 金寨县| 宿迁市| 闸北区| 寻甸|