• Springboot配置异常错误页


    1、在Web项目开发过程中,错误信息提示页是一个重要的组成部分。为了不让用户直接看见异常信息的页面,此时,就需要有一个错误信息提示页。错误页面一般都属于静态页面,这里在src/main/resources/static目录下创建error-404.html。

    首先,在pom.xml里面新增几个配置,在src/main/resources目录下面要加下,不然无法进行加载,修改完毕之后,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>

    搞一个html静态界面放到src/main/resources/static下面,如下所示:

     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>

    建立错误页配置,springboot2.x此类EmbeddedServletContainerCustomizer已经被替换为WebServerFactoryCustomizer,如下所示:

     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 }

    配置完错误页之后,会根据用户请求时的http状态码跳转到不同的页面进行显示。

    运行效果,如下所示:

  • 相关阅读:
    安装mongoDB时,总是报错,启动不了
    koa2路由
    异步操作async await
    nodeJs koa-generator脚手架
    nodeJs学习-19 个人博客案例-(1)数据字典
    nodeJs学习-18 mysql数据库了解
    nodeJs学习-17 博客案例
    nodeJs学习-16 数据字典示例
    前端图片压缩后,文件流上传
    Linux用户名显示-bash-4.1$快速排查
  • 原文地址:https://www.cnblogs.com/biehongli/p/13959028.html
Copyright © 2020-2023  润新知