<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 `@ResponseStatus` > 原文: [http://zetcode.com/springboot/responsestatus/](http://zetcode.com/springboot/responsestatus/) Spring Boot `@ResponseStatus`教程展示了如何在 Spring 應用中使用`@ResponseStatus`注解。 Spring 是流行的 Java 應用框架,而 Spring Boot 是 Spring 的演進,可以幫助輕松地創建獨立的,生產級的基于 Spring 的應用。 ## `@ResponseStatus` `@ResponseStatus`用應返回的狀態代碼和原因消息標記方法或異常類。 調用處理器方法時或拋出指定的異常時,狀態代碼將應用于 HTTP 響應。 它會覆蓋通過其他方式設置的狀態信息,例如`ResponseEntity`或`redirect:`。 ## Spring Boot `@ResponseStatus`示例 在以下應用中,我們演示`@ResponseStatus`注解的用法。 該應用模擬用于通過其 ID 檢索訂單的表單。 嘗試查找 ID 大于 500 的訂單將引發異常。 由于此異常,將顯示一個自定義錯誤頁面。 ```java pom.xml src ├── main │ ├── java │ │ └── com │ │ └── zetcode │ │ ├── Application.java │ │ ├── controller │ │ │ └── MyController.java │ │ └── exception │ │ └── OrderNotFoundException.java │ └── resources │ ├── static │ │ └── index.html │ └── templates │ └── error.ftl └── 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>springbootresponsestatusex</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.1.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-freemarker</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-freemarker`是 Freemarker 模板引擎的依賴項; `spring-boot-maven-plugin`將 Spring 應用打包到可執行的 JAR 或 WAR 歸檔文件中。 `com/zetcode/controller/MyController.java` ```java package com.zetcode.controller; import com.zetcode.exception.OrderNotFoundException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class MyController { @RequestMapping(value = "/orders/{id}") @ResponseBody public String getOrder(@PathVariable("id") long id) { if (id < 0 || id > 500) { var message = String.format("Order %d not found", id); throw new OrderNotFoundException(message); } var message = String.format("Returning order %d", id); return message; } } ``` `MyController`的`getOrder()`方法響應客戶端請求。 它使用`@PathVariable`從路徑讀取順序 ID。 ```java if (id < 0 || id > 500) { var message = String.format("Order %d not found", id); throw new OrderNotFoundException(message); } ``` 對于無效訂單(`id < 0`)和大于 500 的訂單,我們拋出`OrderNotFoundException`異常。 這是訂單系統的簡單模擬。 ```java var message = String.format("Returning order %d", id); ``` 對于其他訂單 ID,我們返回一條消息,指示已找到并返回了該訂單。 `com/zetcode/exception/OrderNotFoundException.java` ```java package com.zetcode.exception; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "No such order") public class OrderNotFoundException extends RuntimeException { public OrderNotFoundException(String message) { super(message); } } ``` 我們有一個自定義`OrderNotFoundException`。 它以`@ResponseStatus`注解裝飾。 該值設置為`HttpStatus.NOT_FOUND`,并且原因消息顯示`"No such order"`。 此信息將在錯誤頁面中使用。 `resources/static/index.html` ```java <!DOCTYPE html> <html> <head> <title>Home page</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <a href="/orders/505/">Get order with Id 505</a> </body> </html> ``` 這是主頁。 它包含一個鏈接,用于查找帶有 ID 505 的訂單。該文件位于`src/main/resources/static`目錄中。 `resources/templates/error.ftl` ```java <html> <head> <title>Error page</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div> <h1>Error occurred</h1> <p>${status}: ${error} - ${message}</p> </div> </body> </html> ``` `error.ftl`是通用錯誤頁面。 這是 Freemarker 模板文件,顯示狀態,錯誤和原因消息。 這些值是通過`@ResponseStatus`設置的。 它位于`src/main/resources/templates`目錄中。 `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); } } ``` `Application`是設置 Spring Boot 應用的入口。 ![Error page](https://img.kancloud.cn/99/fa/99faf618c7880d2ab0270c3647db48eb_516x296.jpg) 圖:錯誤頁面 在本教程中,我們展示了如何在 Spring 應用中使用`@ResponseStatus`注解。 您可能也對相關教程感興趣: [Spring Boot `ResponseEntity`教程](/springboot/responseentity/), [Spring Boot `@ExceptionHandler`教程](/springboot/exceptionhandler/), [Java 教程](/lang/java/)或列出[所有 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>

                              哎呀哎呀视频在线观看