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

How to customize the 404,500 error prompt page by springboot

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

I hope you can read it carefully and be able to achieve something!

Springboot default exception handling mechanism

Springboot already provides a mechanism for handling exceptions by default. Once an exception occurs in the program, springboot sends a request to the url of / error. A class called BasicErrorController is provided in springboot to handle the / error request, and then jump to the page that displays the exception by default to display the exception information

Use the template engine

When using template engines such as thymeleaf, springboot will automatically look for error prompt pages for 404.html and 500.html under the src/main/resources/templates/error/, folder

The naming rule of the error prompt page is: error code .html. For example, 404 is 404. Html.500 is 500.html.

Use the example

Create the springboot project as follows

The page structure of 404 and 500 error tips is as follows

Application.properties project profile

The default value of server.port=8080# is classpath:/templates/, source code spring.mvc.view.prefix=classpath:/templates/# in ThymeleafProperties class, its default value is .html, and the source code is spring.mvc.view.suffix=.htmlspring.thymeleaf.cache=false in ThymeleafProperties class.

The 404 page is as follows

four hundred and four

The contents of the 500 pages are as follows

five hundred

Controller is as follows

@ Controllerpublic class PageController {/ / Jump to the login page @ GetMapping (path = "/ toLogin") public String toLogin () {int code = 1Accord 0; return "login";}}

404.html page test

Access an interface that does not exist: http://localhost:8080/aaaa, and the result is as follows

500.html page test

Access the existing interface: http://localhost:8080/toLogin, and the result is as follows

No template engine is used

If you do not use a template engine such as thymeleaf, springboot will go to the static resource folder to find the error page of 404.htm and 500.html, named as above. There are four static resource paths by default in springboot, which are

Classpath:/METAINF/resources/

Classpath:/resources/

Classpath:/static/

Classpath:/public/

The priority order is: META-INF/resources > resources > static > public. Create an error folder with the above four paths, and then create 404,500 error tips as follows

Without writing additional mappers, you can directly request to the

This is the end of the content of "how to customize the 404,500 error prompt page for springboot". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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