<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Spring Boot `ResponseEntity` > 原文: [http://zetcode.com/springboot/responseentity/](http://zetcode.com/springboot/responseentity/) Spring Boot `ResponseEntity`教程展示了如何在 Spring 應用中使用`ResponseEntity`。 Spring 是流行的 Java 應用框架,而 Spring Boot 是 Spring 的演進,可以幫助輕松地創建獨立的,生產級的基于 Spring 的應用。 ## `ResponseEntity` `ResponseEntity`代表 HTTP 響應,包括標頭,正文和狀態。 盡管`@ResponseBody`將返回值放入響應的正文中,但是`ResponseEntity`也允許我們添加標頭和狀態代碼。 ## Spring Boot `ResponseEntity`示例 在以下應用中,我們演示`ResponseEntity`的用法。 該應用有兩種方法:一種方法使用`ResponseEntity`創建 HTTP 響應,另一種方法`@ResponseBody`。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ ├───controller │ │ │ MyController.java │ │ └───model │ │ Country.java │ └───resources │ └───static │ index.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>responseentityex</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.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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-parent`是父 POM,它為使用 Maven 構建的應用提供依賴關系和插件管理。 `spring-boot-starter-web`是使用 Spring MVC 創建 Spring Boot Web 應用的依賴項。 `spring-boot-maven-plugin`將 Spring 應用打包到可執行的 JAR 或 WAR 歸檔文件中。 `com/zetcode/model/Country.java` ```java package com.zetcode.model; public class Country { private String name; private int population; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPopulation() { return population; } public void setPopulation(int population) { this.population = population; } } ``` 這是`Country` bean。 它具有兩個屬性:`name`和`population`。 `com/zetcode/controller/MyController.java` ```java package com.zetcode.controller; import com.zetcode.bean.Country; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class MyController { @RequestMapping(value = "/getCountry") public ResponseEntity<Country> getCountry() { var c = new Country(); c.setName("France"); c.setPopulation(66984000); var headers = new HttpHeaders(); headers.add("Responded", "MyController"); return ResponseEntity.accepted().headers(headers).body(c); } @RequestMapping(value = "/getCountry2") @ResponseBody public Country getCountry2() { var c = new Country(); c.setName("France"); c.setPopulation(66984000); return c; } } ``` 控制器包含兩種方法。 第一個使用`ResponseEntity`,第二個使用`@ResponseBody`。 ```java @RequestMapping(value = "/getCountry") public ResponseEntity<Country> getCountry() { ``` `getCountry()`方法映射到`getCountry` URL 模式; 它返回類型為`Country`的`ResponseEntity`。 ```java var c = new Country(); c.setName("France"); c.setPopulation(66984000); ``` 我們創建一個`Country` bean; 此 bean 在響應中返回。 ```java var headers = new HttpHeaders(); headers.add("Responded", "MyController"); ``` 我們創建`HttpHeaders`的實例并添加新的標頭值。 ```java return ResponseEntity.accepted().headers(headers).body(c); ``` 返回`ResponseEntity`。 我們給`ResponseEntity`一個自定義狀態代碼,標頭和正文。 ```java @RequestMapping(value = "/getCountry2") @ResponseBody public Country getCountry2() { var c = new Country(); c.setName("France"); c.setPopulation(66984000); return c; } ``` 使用`@ResponseBody`,僅返回正文。 標題和狀態代碼由 Spring 提供。 `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> <p> <a href="getCountry">Get country 1</a> </p> <p> <a href="getCountry2">Get country 2</a> </p> </body> </html> ``` 這是主頁。 它包含兩個鏈接。 `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 應用的入口。 ```java $ curl localhost:8080/getCountry -I HTTP/1.1 202 Responded: MyController Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Date: Thu, 17 Jan 2019 21:40:49 GMT ``` 調用第一個方法時,我們可以看到選擇的 202 狀態代碼和自定義標頭值。 在本教程中,我們展示了如何在 Spring 應用中使用`ResponseEntity`。 您可能也對相關教程感興趣: [Spring Boot `@ResponseStatus`教程](/springboot/responsestatus/), [Spring Boot `@ExceptionHandler`教程](/springboot/exceptionhandler/), [Spring Boot 上傳文件](/springboot/uploadfile/), [Spring Boot `@PathVariable`教程](/springboot/pathvariable/), [Spring Boot `@RequestParam`教程](/springboot/requestparam/), [Spring Boot REST H2 教程](/articles/springbootresth2/),[獨立的 Spring 應用](/articles/standalonespring/), [Java 教程](/lang/java/)。
                  <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>

                              哎呀哎呀视频在线观看