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 目录中放置一个通用的自定义错误页面。
<!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 错误页面。
<!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/ 目录中。
<!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 应用程序的项目结构。
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-web 和 spring-boot-starter-thymeleaf 启动器。
#server.error.whitelabel.enabled=false #spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
在 application.properties 中,我们可以使用这些设置之一来关闭 WhiteLabel 错误。如果我们提供自定义错误页面,它会自动优先于 WhiteLabel 错误。
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";
}
}
我们有一个简单的控制器,它为首页返回文本消息。
<!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 创建的自定义模板错误页面。
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 错误,并展示了如何创建我们的自定义错误页面。