SpringBoot 2.0 已經發布多時,一直不知道它有什麼用,只是知道它有個webflux。今天就來學習一下,看一下是否有必要升級到新版本?
若是你是使用得STS來建立項目的話將會很簡單,直接選擇web flux模塊就好。SpringBoot選擇最新穩定的2.1.4.RELEASE。前端
完整pom文件: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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.mike</groupId> <artifactId>flux</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mike-flux</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <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> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-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>
建立啓動類:(使用STS會自動建立)react
package com.mike; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MikeFluxApplication { public static void main(String[] args) { SpringApplication.run(MikeFluxApplication.class, args); } }
有過前端工做經驗的小夥伴,對路由確定不陌生,Vue react中都有統一的路有管理。如今SpringBoot也能夠這樣來寫了。後端的小夥伴能夠把它理解爲你以前寫的controller。
先定義一個處理類:web
package com.mike.handler; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerResponse; import reactor.core.publisher.Mono; /** * The class HelloHandler * */ @Component public class HelloHandler { public Mono<ServerResponse> sayHello(ServerRequest req) { return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON_UTF8) .body(BodyInserters.fromObject(req.queryParam("name").get())); } }
Mono
定義返回單個結果,定義了響應數據類型以及數據。我是從請求參數中取得name
參數進行返回。spring
有了處理類,咱們就須要定義什麼樣的路由交給它來處理,要將路有何處理程序進行mapping:apache
package com.mike.router; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; import com.mike.handler.HelloHandler; /** * The class HelloRouter * */ @Configuration public class HelloRouter { @Bean public RouterFunction<ServerResponse> hello(HelloHandler handle){ return RouterFunctions.route(RequestPredicates.GET("/hello") .and(RequestPredicates.accept(MediaType.APPLICATION_JSON_UTF8)) ,handle::sayHello); } }
咱們定義了/hello
的請求交給咱們的處理類去處理,這樣一次完整的請求就搞定了。啓動程序,訪問http://localhost:8080/hello?name=mike 就能夠看到頁面上的結果了。編程
Mono
或Flux
來包裹。controller
。關於webflux若是你有更深的理解,但願能夠回覆一塊兒交流。後端
若是想要學習更多知識,能夠關注下個人公衆號:
api