jdk -> 1.8前端
maven -3.5 java
springboot -> 2.0.3react
開發工具 -> IDEAweb
利用 IDEA 或者 start.spring.io 建立一個SpringBoot項目spring
<?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>cn.xiangxu</groupId> <artifactId>test_demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>test_demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <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-devtools</artifactId> <!--<scope>runtime</scope>--> <optional>true</optional> </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> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project>
至關於SpringMVC中的service類,主要就是根據不一樣的請求url執行不一樣的業務邏輯apache
技巧01:編寫的處理器類必須被容器所管理springboot
package cn.xiangxu.test_demo.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; /** * @author 王楊帥 * @create 2018-06-26 9:58 * @desc 至關於springMVC的controller類 **/ @Component public class StudentHandler { public Mono<ServerResponse> helloStudent(ServerRequest serverRequest) { return ServerResponse // 響應對象封裝 .ok() // 響應碼 .contentType(MediaType.TEXT_PLAIN) // 響應類型 .body(BodyInserters.fromObject("這是學生控制類")); // 響應體 } }
至關於DispatherServlet,主要就是將不一樣的請求url和對應的處理器類進行匹配maven
技巧01:路由器類至關於一個Java配置類,因此必須添加 @Configuration 註解,而且路由器類中的路由方法返回的對象必須被容器管理,因此必須添加@Bean註解ide
技巧02:RouterFunctions的route方法接收兩個參數,並返回 RouterFunctions 類型函數
》參數一 :請求斷言
》參數二 :處理函數接口【可用lambda表達式代替】
package cn.xiangxu.test_demo.router; import cn.xiangxu.test_demo.handler.StudentHandler; 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; /** * @author 王楊帥 * @create 2018-06-26 10:02 * @desc **/ @Configuration public class StudentRouter { @Bean public RouterFunction<ServerResponse> routerHelloStudent(StudentHandler studentHandler) { return RouterFunctions .route( // 路由匹配:將請求url路由到對應的處理器 RequestPredicates .GET("/student/hello") // 請求url .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), // 接收的請求數據類型 studentHandler::helloStudent // 處理請求的處理方法 ); /* 代碼解釋:若是請求是/student/hello時就會調用StudentHandler處理器類中的helloStudent方法進行業務邏輯處理 */ } }
直接運行啓動類就能夠啦
》打包項目
》運行項目
進入到項目根目錄下的target文件夾後經過 java -jar xxx.jar 就能夠運行項目啦
技巧01:SpringBoot WebFlux 默認集成的是 Netty