<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 `RouterFunction` 教程 > 原文: [http://zetcode.com/springboot/routerfunction/](http://zetcode.com/springboot/routerfunction/) Spring Boot `RouterFunction`教程展示了如何在 Spring Boot 應用中創建函數式路由。 ## 反應式編程 反應式編程是一種編程范例,它是函數式的,基于事件的,非阻塞的,異步的,并且以數據流處理為中心。 術語反應式來自以下事實:我們對諸如鼠標單擊或 I/O 事件之類的更改做出反應。 傳統的 Spring MVC 應用使用諸如`@GetMapping`之類的注解將請求路徑映射到控制器動作。 函數式路由 API 是此映射的替代方法。 ## `RouterFunction` `RouterFunction`表示路由到處理器函數的函數。 ## Spring Boot `RouterFunction` 示例 在以下應用中,我們創建具有函數式路由的反應式 Spring Boot 應用。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ └───routes │ │ MyRoutes.java │ └───resources └───test └───java └───com └───zetcode └───routes MyRoutesTest.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>springbootrouterfunction</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.5.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` 這是 Maven `pom.xml`文件。 `RouterFunction`依賴于`spring-boot-starter-webflux`。 `com/zetcode/routes/MyRoutes.java` ```java package com.zetcode.routes; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.ServerResponse; import static org.springframework.web.reactive.function.BodyInserters.fromObject; import static org.springframework.web.reactive.function.server.RequestPredicates.GET; import static org.springframework.web.reactive.function.server.RouterFunctions.route; import static org.springframework.web.reactive.function.server.ServerResponse.ok; @Configuration public class MyRoutes { @Bean RouterFunction<ServerResponse> home() { return route(GET("/"), request -> ok().body(fromObject("Home page"))); } @Bean RouterFunction<ServerResponse> about() { return route(GET("/about"), request -> ok().body(fromObject("About page"))); } } ``` 我們定義了兩個函數式路由。 ```java @Bean RouterFunction<ServerResponse> home() { return route(GET("/"), request -> ok().body(fromObject("Home page"))); } ``` 通過函數式路由,我們可以編寫簡單而優雅的代碼。 在這里,我們返回主頁的簡單文本消息。 `com/zetcode/routes/MyRoutesTest.java` ```java package com.zetcode.routes; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.reactive.server.WebTestClient; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class MyRoutesTest { @Autowired private WebTestClient client; @Test public void test_home_page() { client.get().uri("/").exchange().expectStatus().isOk() .expectBody(String.class).isEqualTo("Home page"); } @Test public void test_about_page() { client.get().uri("/about").exchange().expectStatus().isOk() .expectBody(String.class).isEqualTo("About page"); } } ``` 使用`WebTestClient`,我們測試了兩個路由。 `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 應用。 ```java $ mvn spring-boot:run ``` 我們運行該應用并導航到`localhost:8080`。 在本教程中,我們學習了如何通過`RouterFunction`使用函數式路由。 列出[所有 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>

                              哎呀哎呀视频在线观看