ZetCode

Spring Boot Whitelabel 错误

最后修改于 2023 年 7 月 16 日

Spring Boot Whitelabel 错误教程展示了如何在 Spring Boot 应用程序中配置和显示错误消息。

Spring 是一个流行的 Java 应用程序框架,而 Spring Boot 是 Spring 的一个演进,它有助于轻松创建独立的、生产级别的基于 Spring 的应用程序。

WhiteLabel 错误页面

WhiteLabel 错误页面是一个通用的 Spring Boot 错误页面,当没有自定义错误页面时显示。

server.error.whitelabel.enabled=false

可以在 application.properties 文件中通过将 server.error.whitelabel.enabled 设置为 false 来禁用 WhiteLabel 错误。

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration

另一种禁用 WhiteLabel 错误的方法是排除 ErrorMvcAutoConfiguration

@SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class})
public class Application {

或者,排除可以在注解中完成。

当 WhiteLabel 错误页面被禁用且未提供自定义错误页面时,将显示 Web 服务器的错误页面(Tomcat,Jetty)。

Spring Boot 自定义错误页面

在不使用 Thymeleaf 模板引擎的情况下,我们可以在 src/main/resources/public/errors 目录中放置一个通用的自定义错误页面。

resources/public/errors/404.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>404 - resource not found</title>
</head>
<body>

<h2>404 - Resource not found</h2>

<p>
The requested resource was not found; - public
</p>

</body>
</html>

这是一个通用的 404 错误页面。

resources/templates/error.html
<!DOCTYPE html>
<html>
<head>
    <title>Error occurred</title>
</head>
<body>
<h1>Error occurred</h1>

<p>
    An error has occurred. Please contact the administrator; - template generic
</p>

</body>
</html>

使用模板的通用错误页面可以放置在 src/main/resources/templates/ 目录中。

resources/templates/error/404.html
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>404 - resource not found</title>
</head>
<body>

<h2>404 - Resource not found</h2>

<p>
The requested resource was not found; template - specific
</p>

<p th:text="${error}">Error Info</p>
<p th:text="${status}">Status</p>

</body>
</html>

使用模板的特定错误页面可以放置在 src/main/resources/templates/error/ 目录中。

Spring Boot 自定义错误页面示例

在下面的例子中,我们创建了一个简单的 Spring Boot 应用程序,该应用程序使用自定义错误页面来处理 404 错误。

build.gradle
...
src
├───main
│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           │   Application.java
│   │           └───controller
│   │                   MyController.java
│   └───resources
│       │   application.properties
│       └───templates
│           └───error
│                   404.html
└───test
    └───java

这是 Spring 应用程序的项目结构。

build.gradle
plugins {
    id 'org.springframework.boot' version '3.1.1'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'java'
}

group = 'com.zetcode'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
}

test {
    useJUnitPlatform()
}

这是 Gradle 构建文件。我们有 spring-boot-starter-webspring-boot-starter-thymeleaf 启动器。

resources/application.properties
#server.error.whitelabel.enabled=false
#spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration

application.properties 中,我们可以使用这些设置之一来关闭 WhiteLabel 错误。如果我们提供自定义错误页面,它会自动优先于 WhiteLabel 错误。

com/zetcode/controller/MyController.java
package com.zetcode.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @GetMapping("/")
    public String home() {

        return "Home page";
    }
}

我们有一个简单的控制器,它为首页返回文本消息。

resources/templates/error/404.html
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>404 - resource not found</title>
</head>
<body>

<h2>404 - Resource not found</h2>

<p>
The requested resource was not found; template - specific
</p>

<p th:text="${error}">Error Info</p>
<p th:text="${status}">Status</p>

</body>
</html>

这是一个用 Thymeleaf 创建的自定义模板错误页面。

com/zetcode/Application.java
package com.zetcode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application  {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

此代码设置了 Spring Boot 应用程序。

在本文中,我们介绍了 WhiteLabel 错误,并展示了如何创建我们的自定义错误页面。

作者

我叫 Jan Bodnar,是一位对编程充满热情的程序员,拥有丰富的编程经验。自 2007 年以来,我一直在撰写编程文章。迄今为止,我撰写了 1,400 多篇文章和 8 本电子书。我拥有超过十年的编程教学经验。

列出 所有 Spring Boot 教程