Springboot configuration exception error page

1. In the process of Web project development, the error message prompt page is an important part. In order to prevent the user from directly seeing the abnormal information page, at this time, an error information prompt page is required. Error pages are generally static pages. Here, create error-404.html in the src/main/resources/static directory.

First of all, add a few configurations in pom.xml, add them under the src/main/resources directory, otherwise it cannot be loaded, after the modification is completed, click on Maven -> Update Project.

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 5     https://maven.apache.org/xsd/maven-4.0.0.xsd">
 6     <modelVersion>4.0.0</modelVersion>
 7     <parent>
 8         <groupId>org.springframework.boot</groupId>
 9         <artifactId>spring-boot-starter-parent</artifactId>
10         <version>2.3.5.RELEASE</version>
11         <relativePath /> <!-- lookup parent from repository -->
12     </parent>
13     <groupId>com.example</groupId>
14     <artifactId>demo</artifactId>
15     <version>0.0.1-SNAPSHOT</version>
16     <name>demo</name>
17     <description>Demo project for Spring Boot</description>
18 
19     <properties>
20         <java.version>1.8</java.version>
21         <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
22     </properties>
23 
24     <dependencies>
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-web</artifactId>
28         </dependency>
29 
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-starter-test</artifactId>
33             <scope>test</scope>
34             <exclusions>
35                 <exclusion>
36                     <groupId>org.junit.vintage</groupId>
37                     <artifactId>junit-vintage-engine</artifactId>
38                 </exclusion>
39             </exclusions>
40         </dependency>
41         <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
42         <dependency>
43             <groupId>org.hibernate</groupId>
44             <artifactId>hibernate-validator</artifactId>
45             <version>6.1.0.Final</version>
46         </dependency>
47     </dependencies>
48 
49     <build>
50         <plugins>
51             <plugin>
52                 <groupId>org.springframework.boot</groupId>
53                 <artifactId>spring-boot-maven-plugin</artifactId>
54             </plugin>
55         </plugins>
56         <resources>
57             <resource>
58                 <directory>src/main/resources</directory>
59                 <includes>
60                     <include>**/*.properties</include>
61                     <include>**/*.yml</include>
62                     <include>**/*.xml</include>
63                     <include>**/*.p12</include>
64                     <include>**/*.html</include>
65                     <include>**/*.jpg</include>
66                     <include>**/*.png</include>
67                 </includes>
68             </resource>
69         </resources>
70     </build>
71 
72 </project>

Make an html static interface and put it under src/main/resources/static, as shown below:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 
 9     <img alt="错误页面" src="images/error.jpg">
10 
11 </body>
12 </html>

Establish error page configuration, Springboot2.x EmbeddedServletContainerCustomizer has been replaced with WebServerFactoryCustomizer, as shown below:

 1 package com.demo.config;
 2 
 3 import org.springframework.boot.web.server.ConfigurableWebServerFactory;
 4 import org.springframework.boot.web.server.ErrorPage;
 5 import org.springframework.boot.web.server.WebServerFactoryCustomizer;
 6 import org.springframework.context.annotation.Bean;
 7 import org.springframework.context.annotation.Configuration;
 8 import org.springframework.http.HttpStatus;
 9 
10 /**
11  * 
12  * @author
13  * 
14  *         springboot2.x此类EmbeddedServletContainerCustomizer已经被替换为WebServerFactoryCustomizer
15  * 
16  */
17 @Configuration
18 public class ErrorPageConfig {
19 
20     @Bean
21     public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
22         WebServerFactoryCustomizer<ConfigurableWebServerFactory> customizer = new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
23 
24             @Override
25             public void customize(ConfigurableWebServerFactory factory) {
26                 // 定义404错误页
27                 HttpStatus notFound = HttpStatus.NOT_FOUND;
28                 System.out.println(notFound);
29                 // 定义404错误页
30                 ErrorPage errorPage404 = new ErrorPage(notFound, "/error-404.html");
31                 // 追加错误页,替换springboot默认的错误页
32                 factory.addErrorPages(errorPage404);
33                 // 设置tomcat服务器的端口号
34                 factory.setPort(8081);
35             }
36 
37         };
38         return customizer;
39     }
40 
41 }

After configuring the error page, it will jump to a different page for display according to the http status code when the user requests it.

The running effect is as follows:

Guess you like

Origin blog.csdn.net/u011652364/article/details/109713306