Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways for SpringBoot to customize error pages?

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the ways of customizing error pages in SpringBoot". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the ways of customizing error pages in SpringBoot.

Previous web.xml mode

Let's first take a look at how to configure the error page in web.xml:

Implementation method in 404 / error/404.jspSpringBoot

After SpringBoot, you can implement custom error pages in several ways.

1. Implement the bean of EmbeddedServletContainerCustomizer

Suitable for embedded servers, first define our error page Mapping in controller, implement the bean of EmbeddedServletContainerCustomizer in the configuration class, and add the error page corresponding to the status code. Note that this method will fail when it is typed into war and used by external tomcat.

Define the error page:

@ RequestMapping (value = "/ error/ [code]") public String error (@ PathVariable int code, Model model) {String pager = "/ content/error-pager"; switch (code) {case 404: model.addAttribute ("code", 404); pager = "/ content/error-pager"; break Case 500: model.addAttribute ("code", 500); pager = "/ content/error-pager"; break;} return pager;}

Add EmbeddedServletContainerCustomizer to the configuration class:

/ * configure the default error page (for embedded tomcat startup only) * using this method, it will not work after being packaged as war * * @ return * / @ Beanpublic EmbeddedServletContainerCustomizer containerCustomizer () {return container-> {ErrorPage error404Page = new ErrorPage (HttpStatus.NOT_FOUND, "/ error/404") ErrorPage error500Page = new ErrorPage (HttpStatus.INTERNAL_SERVER_ERROR, "/ error/500"); container.addErrorPages (error404Page, error500Page);}; 2. Through the interceptor mode

Suitable for embedded Tomcat or war mode.

/ * * @ author hgs * @ version ErrorPageInterceptor.java, v 0.1 2018-03-04 20:52 hgs Exp $*

* error page interceptor * replaces the method that EmbeddedServletContainerCustomizer does not work in war * / @ Componentpublic class ErrorPageInterceptor extends HandlerInterceptorAdapter {private List errorCodeList = Arrays.asList (404403,500,501); @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {if (errorCodeList.contains (response.getStatus () {response.sendRedirect ("/ error/" + response.getStatus ()); return false } return super.preHandle (request, response, handler);}}

Add intercept to the configuration class

@ Configurationpublic class WebMvcConfig extends WebMvcConfigurerAdapter {@ Override public void addInterceptors (InterceptorRegistry registry) {registry.addInterceptor (errorPageInterceptor); / / .addPathPatterns ("/ action/**", "/ mine/**"); default to all super.addInterceptors (registry);}} 3. Custom static error page method

Add an error.html page under resource/templates and springBoot will automatically find the page as an error page, which is suitable for embedded Tomcat or war.

The SpringBoot error view provides the following error properties:

Timestamp: when the error occurred

Status:HTTP status?

Error: cause of error

Exception: the class name of the exception

Message: exception message (if the error is caused by an exception)

Various errors in the errors:BindingResult exception (if the error is caused by the exception)

Trace: exception tracking information (if the error is caused by an exception)

Path: the URL path requested when the error occurred.

The name of the page varies according to the front-end framework template used by SpringBoot:

The ID of the Bean that implements the View interface of Spring needs to be set to error (parsed by the BeanNameViewResolver of Spring)

If Thymeleaf is configured, you need a Thymeleaf template named error.html

If FreeMarker is configured, you need a FreeMarker template named error.ftl

If Velocity is configured, you need a Velocity template named error.vm

If you are using the JSP view, you need a JSP template named error.jsp.

Thymeleaf instance:

I'm sorry, I can't find the page you want to view.

Go back to the front page

The third solution in external Tomcat is a recommended implementation, but it is not flexible enough, so it is difficult to define our own attributes. If you want to modify it accordingly, you can refer to the source code BasicErrorController to achieve the desired effect by inheriting AbstractErrorController and rewriting the errorHtml method. When embedded with Tomcat, the first one is recommended for more flexibility.

Thank you for your reading, the above is the "SpringBoot custom error page way what" the content, after the study of this article, I believe you have a deeper understanding of the SpringBoot custom error page way what this problem, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report