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 solve the configuration of 404,500 exception pages in SpringBoot

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "how to solve the configuration of 404,500 abnormal pages in SpringBoot". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

i. Environment building

First of all, you have to build a web application before it is possible to continue the follow-up testing. Building a web application with SpringBoot is a relatively simple task.

Create a maven project with the following pom file

Org.springframework.boot spring-boot-starter-parent 2.1.7 UTF-8 UTF-8 Finchley.RELEASE 1.8 org.springframework.boot spring-boot-starter-web com.alibaba fastjson 1.2.45 org.springframework.boot spring-boot-maven-plugin Spring-milestones Spring Milestones https://repo.spring.io/milestone false

It's still a normal process. After the pom dependency is done, write a program entry.

/ * Created by @ author yihui in 15:26 19-9-13. * / @ SpringBootApplicationpublic class Application {public static void main (String [] args) {SpringApplication.run (Application.class);}} II. Exception page configuration

In the SpringBoot project, it provides a default exception handling page, what can we do when we want to use custom pages such as 404500?

1. Default exception page configuration

By default, it is very easy to configure the exception page. Under the resource path, create a new error directory, add 400.html below, and add the 500html page.

The project structure is as above, note that the example demo here does not use a template engine, so our exception page is placed in the static directory; if you use a template engine such as FreeMaker, you can put the error template page in the template directory

Next, to actually test whether it works, let's first define a service that may have server 500.

@ Controller@RequestMapping (path = "page") public class ErrorPageRest {@ ResponseBody @ GetMapping (path = "divide") public int divide (int sub) {System.out.println ("divide1"); return 1000 / sub;}}

Request a url that does not exist and return to the 400.html page we defined

404 page does not exist

Request a server 500 exception and return to the 500.html page we defined

An exception occurred on the 500-page server!

2. BasicErrorController

Look at the use of the above is relatively simple, there will be a question, how does this exception page return?

From the project startup log, pay attention to RequestMappingHandlerMapping

We can find that there is a path to / error that is not defined by us. From a naming point of view, this is probably a Controller-> BasicErrorController specifically used to handle exceptions. Part of the code is as follows

@ Controller@RequestMapping ("${server.error.path:$ {error.path:/error}}") public class BasicErrorController extends AbstractErrorController {@ Override public String getErrorPath () {return this.errorProperties.getPath ();} @ RequestMapping (produces = "text/html") public ModelAndView errorHtml (HttpServletRequest request, HttpServletResponse response) {HttpStatus status = getStatus (request) Map model = Collections.unmodifiableMap (getErrorAttributes (request, isIncludeStackTrace (request, MediaType.TEXT_HTML)); response.setStatus (status.value ()); ModelAndView modelAndView = resolveErrorView (request, response, status, model); return (modelAndView! = null)? ModelAndView: new ModelAndView ("error", model);} @ RequestMapping @ ResponseBody public ResponseEntity error (HttpServletRequest request) {Map body = getErrorAttributes (request, isIncludeStackTrace (request, MediaType.ALL); HttpStatus status = getStatus (request); return new ResponseEntity (body, status);}}

In this Controller, there is an interface that returns the web page and an interface that returns the Json string; the one we used before should be the first, so in what scenario will we use the second one?

By defining the Accept of the request header, we only want to get the return of json.

3. Summary

This article is relatively simple and can be summarized into two sentences as follows

Name the custom exception page according to the http status code and put it in the / error directory

In case of an exception, find the corresponding exception page according to the returned http status code and return

This is the end of the content of "how to solve the configuration of 404,500 exception pages in SpringBoot". Thank you for your 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

Internet Technology

Wechat

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

12
Report