In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use springboot ErrorPageFilter". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use springboot ErrorPageFilter.
Practical Application of ErrorPageFilter Spring Framework error Page filter
Springboot provides an ErrorPageFilter to deal with how to show errors when an error occurs in a program. Look at the code without saying much.
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 ()) {/ / focus on this method 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 } / / get the error page and follow that the property this.statuses is a map, and the error page is obtained from this property. When was the content of this property added? String errorPath = getErrorPath (this.statuses, status); if (errorPath = = null) {response.sendError (status, message); return;} response.setStatus (status); setErrorAttributes (request, status, message) / / after getting the address of the error page, jump to the error page request.getRequestDispatcher (errorPath) .forward (request, response) by server redirection;}
ErrorPageFilter implements Filter, ErrorPageRegistry, this class implements the ErrorPageRegistry interface. The methods in the interface are as follows. We can see that the input parameter errorPages is the collection of error pages, and then put all the error pages into the statuses property, but where does the input parameter come from?
@ 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 ());}
Through the source code analysis, found that this interface, as long as the implementation of this interface and generate bean to spring, you can add your own error page to ErrorPageRegistry.
Public interface ErrorPageRegistrar {/ * * Register pages as required with the given registry. * @ param registry the error page registry * / void registerErrorPages (ErrorPageRegistry registry);}
Look at an example, this is fine, is not very simple.
@ 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);}} ErrorPageFilter exception occurred in springboot project
Today, when I use the springboot (2.2.12.RELEASE) + beetl template, because a template cannot be found
The system has been reporting the wrong day.
[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
After looking at some solutions on the Internet, we generally rewrite ErrorPageFilter, and then set filterRegistrationBean.setEnabled (false) in FilterRegistrationBean.
The code is @ 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;}
According to this method, I tried many times, and the system directly reported that there was an errorPageFilter conflict, which turned out to be
A bean has been defined in ErrorPageFilterConfiguration.java:
/ 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;}}
Finally, my solution is to set in the startup class:
@ SpringBootApplicationpublic class App extends SpringBootServletInitializer {public App () {super (); / / set to false setRegisterErrorPageFilter (false);} @ Overrideprotected SpringApplicationBuilder configure (SpringApplicationBuilder application) {return application.sources (App.class);} public static void main (String [] args) {SpringApplication.run (App.class, args);} so far, I believe you have a deeper understanding of "how to use springboot ErrorPageFilter", so you might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.