From 5412e335984b5d22e6534168630ffed8403d5ef1 Mon Sep 17 00:00:00 2001 From: liqiangqiang Date: Sun, 8 Apr 2018 16:25:02 +0800 Subject: [PATCH] =?UTF-8?q?WebFlux=20=E5=BF=AB=E9=80=9F=E5=85=A5=E9=97=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- springboot-webflux-1-quickstart/pom.xml | 41 +++++++++++++ .../org/spring/springboot/Application.java | 21 +++++++ .../org/spring/springboot/domain/City.java | 61 +++++++++++++++++++ .../springboot/handler/CityHandler.java | 17 ++++++ .../spring/springboot/router/CityRouter.java | 24 ++++++++ .../src/main/resources/application.properties | 0 6 files changed, 164 insertions(+) create mode 100755 springboot-webflux-1-quickstart/pom.xml create mode 100644 springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/Application.java create mode 100644 springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/domain/City.java create mode 100644 springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/handler/CityHandler.java create mode 100644 springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/router/CityRouter.java create mode 100644 springboot-webflux-1-quickstart/src/main/resources/application.properties diff --git a/springboot-webflux-1-quickstart/pom.xml b/springboot-webflux-1-quickstart/pom.xml new file mode 100755 index 0000000..64eea95 --- /dev/null +++ b/springboot-webflux-1-quickstart/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + springboot + springboot-webflux-1-quickstart + 0.0.1-SNAPSHOT + springboot-webflux-1-quickstart :: Spring Boot WebFlux 快速入门 + + + + org.springframework.boot + spring-boot-starter-parent + 2.0.1.RELEASE + + + + + + + org.springframework.boot + spring-boot-starter-webflux + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + junit + junit + 4.12 + + + + diff --git a/springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/Application.java b/springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/Application.java new file mode 100644 index 0000000..a472cd5 --- /dev/null +++ b/springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/Application.java @@ -0,0 +1,21 @@ +package org.spring.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Spring Boot 应用启动类 + * + * Created by bysocket on 09/29/2017. + */ +// Spring Boot 应用的标识 +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + // 程序启动入口 + // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件 + SpringApplication.run(Application.class,args); + + } +} diff --git a/springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/domain/City.java b/springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/domain/City.java new file mode 100644 index 0000000..598af6d --- /dev/null +++ b/springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/domain/City.java @@ -0,0 +1,61 @@ +package org.spring.springboot.domain; + +/** + * 城市实体类 + * + * Created by bysocket on 09/29/2017. + */ +public class City { + + /** + * 城市编号 + */ + private Long id; + + /** + * 省份编号 + */ + private Long provinceId; + + /** + * 城市名称 + */ + private String cityName; + + /** + * 描述 + */ + private String description; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getProvinceId() { + return provinceId; + } + + public void setProvinceId(Long provinceId) { + this.provinceId = provinceId; + } + + public String getCityName() { + return cityName; + } + + public void setCityName(String cityName) { + this.cityName = cityName; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} diff --git a/springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/handler/CityHandler.java b/springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/handler/CityHandler.java new file mode 100644 index 0000000..7d04624 --- /dev/null +++ b/springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/handler/CityHandler.java @@ -0,0 +1,17 @@ +package org.spring.springboot.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; + +@Component +public class CityHandler { + + public Mono helloCity(ServerRequest request) { + return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN) + .body(BodyInserters.fromObject("Hello, City!")); + } +} diff --git a/springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/router/CityRouter.java b/springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/router/CityRouter.java new file mode 100644 index 0000000..fbf938c --- /dev/null +++ b/springboot-webflux-1-quickstart/src/main/java/org/spring/springboot/router/CityRouter.java @@ -0,0 +1,24 @@ +package org.spring.springboot.router; + +import org.spring.springboot.handler.CityHandler; +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; + +@Configuration +public class CityRouter { + + + @Bean + public RouterFunction routeCity(CityHandler cityHandler) { + return RouterFunctions + .route(RequestPredicates.GET("/hello") + .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), + cityHandler::helloCity); + } + +} diff --git a/springboot-webflux-1-quickstart/src/main/resources/application.properties b/springboot-webflux-1-quickstart/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29