💎一站式轻松地调用各大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/)。