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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Spring Boot `@RestController` > 原文: [http://zetcode.com/springboot/restcontroller/](http://zetcode.com/springboot/restcontroller/) Spring Boot `@RestController`教程展示了如何在 Spring 應用中使用`@RestController`注解來構建 Restful 控制器。 Spring 是流行的 Java 應用框架,而 Spring Boot 是 Spring 的演進,可以幫助輕松地創建獨立的,生產級的基于 Spring 的應用。 ## Spring MVC Spring MVC 是基于 Servlet API 構建的主要 Web 框架。 它基于流行的 MVC 設計模式。 MVC(模型-視圖-控制器)是一種軟件架構模式,它將應用分為三個區域:模型,視圖和控制器。 該模型表示一個攜帶數據的 Java 對象。 該視圖表示模型包含的數據的可視化。 控制器控制數據流到模型對象中,并在數據更改時更新視圖。 它將視圖和模型分開。 Spring Framework 5.0 引入了一個名為 Spring WebFlux 的并行反應式棧 Web 框架。 ## `@RestController` `@RestController`是用于創建 Restful 控制器的便捷注解。 它是`@Component`的特化,可通過類路徑掃描自動檢測。 它添加了`@Controller`和`@ResponseBody`注解。 它將響應轉換為 JSON 或 XML。 它不適用于視圖技術,因此方法無法返回`ModelAndView`。 它通常與基于`@RequestMapping`注解的注解處理器方法結合使用。 `@Controller`注解與視圖技術一起使用。 ## RESTFul 應用 RESTFul 應用遵循 REST 架構樣式,該樣式用于設計網絡應用。 RESTful 應用生成對資源執行 CRUD(創建/讀取/更新/刪除)操作的 HTTP 請求。 RESTFul 應用通常以 JSON 或 XML 格式返回數據。 ## Spring Boot `@RestController`示例 在以下應用中,我們演示`@RestController`的用法。 該應用返回城市列表作為 JSON 數據。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ ├───controller │ │ │ MyController.java │ │ ├───model │ │ │ City.java │ │ └───service │ │ CityService.java │ │ ICityService.java │ └───resources │ └───static │ index.html └───test └───java ``` 這是項目結構。 `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>springbootrestcontrollerex</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> </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 構建 Web(包括 RESTful)應用的入門工具。 它使用 Tomcat 作為默認的嵌入式容器。 `spring-boot-maven-plugin`將 Spring 應用打包到可執行的 JAR 或 WAR 歸檔文件中。 `com/zetcode/model/City.java` ```java package com.zetcode.model; import java.util.Objects; public class City { private Long id; private String name; private int population; public City() { } public City(Long id, String name, int population) { this.id = id; this.name = name; this.population = population; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } 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; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; City city = (City) o; return population == city.population && Objects.equals(id, city.id) && Objects.equals(name, city.name); } @Override public int hashCode() { return Objects.hash(id, name, population); } @Override public String toString() { final StringBuilder sb = new StringBuilder("City{"); sb.append("id=").append(id); sb.append(", name='").append(name).append('\''); sb.append(", population=").append(population); sb.append('}'); return sb.toString(); } } ``` 這是一個`City` bean。 它具有`id`,`name`和`population`屬性。 `com/zetcode/controller/MyController.java` ```java package com.zetcode.controller; import com.zetcode.model.City; import com.zetcode.service.ICityService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class MyController { @Autowired private ICityService cityService; @GetMapping(value = "/cities") public List<City> getCities() { List<City> cities = cityService.findAll(); return cities; } } ``` 這是`MyController`。 它以 JSON 格式返回城市列表。 ```java @RestController public class MyController { ``` `MyController`帶有`@RestController`注解。 ```java @Autowired private ICityService cityService; ``` 我們用`@Autowired`注解注入`CityService`。 ```java @GetMapping(value = "/cities") public List<City> getCities() { ``` `getCities()`方法映射到`getCities` URL 模式; 它返回城市列表,并通過消息轉換器將其轉換為 JSON。 `com/zetcode/service/ICityService.java` ```java package com.zetcode.service; import com.zetcode.bean.City; import java.util.List; public interface ICityService { public List<City> findAll(); } ``` `ICityService`包含`findAll()`合約方法。 `com/zetcode/service/CityService.java` ```java package com.zetcode.service; import com.zetcode.model.City; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Service; @Service public class CityService implements ICityService { @Override public List<City> findAll() { var cities = new ArrayList<City>(); cities.add(new City(1L, "Bratislava", 432000)); cities.add(new City(2L, "Budapest", 1759000)); cities.add(new City(3L, "Prague", 1280000)); cities.add(new City(4L, "Warsaw", 1748000)); cities.add(new City(5L, "Los Angeles", 3971000)); cities.add(new City(6L, "New York", 8550000)); cities.add(new City(7L, "Edinburgh", 464000)); cities.add(new City(8L, "Berlin", 3671000)); return cities; } } ``` `CityService`包含`findAll()`方法的實現。 `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="cities">Get all cities</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 應用的入口。 `@SpringBootApplication`注解啟用自動配置和組件掃描。 在掃描過程中,將查找`@RestController`注解,并從`MyController`類創建一個 Spring bean。 ```java $ mvn spring-boot:run ``` 應用運行后,我們可以導航到`localhost:8080`。 在本教程中,我們展示了如何在 Spring 應用中使用`@RestController`注解。 您可能也對相關教程感興趣: [Spring Boot `RestTemplate`教程](/springboot/resttemplate/), [Spring Boot `@Controller`教程](/springboot/controller/), [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>

                              哎呀哎呀视频在线观看