<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Spring Boot Whitelabel 錯誤 > 原文: [http://zetcode.com/springboot/whitelabelerror/](http://zetcode.com/springboot/whitelabelerror/) Spring Boot Whitelabel 錯誤教程展示了如何在 Spring Boot 應用中配置和顯示錯誤消息。 Spring 是流行的 Java 應用框架,而 Spring Boot 是 Spring 的演進,可以幫助輕松地創建獨立的,生產級的基于 Spring 的應用。 ## WhiteLabel 錯誤頁面 WhiteLabel 錯誤頁面是通用的 Spring Boot 錯誤頁面,當不存在自定義錯誤頁面時顯示。 ```java server.error.whitelabel.enabled=false ``` 通過將`server.error.whitelabel.enabled`設置為`false`,可以在`application.properties`文件中禁用 WhiteLabel 錯誤。 ```java spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration ``` 禁用 WhiteLabel 錯誤的另一種方法是排除`ErrorMvcAutoConfiguration`。 ```java @SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class}) public class Application { ``` 或者,可以在注解中進行排除。 當禁用 WhiteLabel 錯誤頁面并且未提供自定義錯誤頁面時,將顯示 Web 服務器的錯誤頁面(Tomcat,Jetty)。 ## Spring Boot 自定義錯誤頁面 如果不使用 Thymeleaf 的模板引擎,我們可以在一個`src/main/resources/public/errors`目錄放置一個普通的自定義錯誤頁。 `resources/public/errors/404.html` ```java <!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` ```java <!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` ```java <!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 錯誤的自定義錯誤頁面。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ └───controller │ │ MyController.java │ └───resources │ │ application.properties │ └───templates │ └───error │ 404.html └───test └───java ``` 這是 Spring 應用的項目結構。 `pom.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>springbootwhitelabelerror</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` 這是 Maven `pom.xml`文件。 我們有`spring-boot-starter-web`和`spring-boot-starter-thymeleaf`。 `resources/application.properties` ```java #server.error.whitelabel.enabled=false #spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration ``` 在`application.properties`中,我們可以使用這些設置中的`on`來關閉 WhiteLabel 錯誤。 如果我們提供一個自定義錯誤頁面,它將自動優先于 WhiteLabel 錯誤。 `com/zetcode/controller/MyController.java` ```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` ```java <!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` ```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 錯誤,并展示了如何創建自定義錯誤頁面。 列出[所有 Spring Boot 教程](/all/#springboot)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看