From 5f0ebef6f35639ed22d732fe33eb65092de82309 Mon Sep 17 00:00:00 2001 From: liqiangqiang Date: Fri, 4 May 2018 10:21:40 +0800 Subject: [PATCH] =?UTF-8?q?WebFlux=20=E9=9B=86=E6=88=90=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=8F=8A=E9=83=A8=E7=BD=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- springboot-webflux-9-test/pom.xml | 53 ++++++++++++++++ .../org/spring/springboot/Application.java | 20 ++++++ .../spring/springboot/dao/CityRepository.java | 10 +++ .../org/spring/springboot/domain/City.java | 63 +++++++++++++++++++ .../springboot/handler/CityHandler.java | 43 +++++++++++++ .../controller/CityWebFluxController.java | 41 ++++++++++++ .../src/main/resources/application.properties | 5 ++ .../springboot/handler/CityHandlerTest.java | 52 +++++++++++++++ 8 files changed, 287 insertions(+) create mode 100755 springboot-webflux-9-test/pom.xml create mode 100644 springboot-webflux-9-test/src/main/java/org/spring/springboot/Application.java create mode 100644 springboot-webflux-9-test/src/main/java/org/spring/springboot/dao/CityRepository.java create mode 100644 springboot-webflux-9-test/src/main/java/org/spring/springboot/domain/City.java create mode 100644 springboot-webflux-9-test/src/main/java/org/spring/springboot/handler/CityHandler.java create mode 100644 springboot-webflux-9-test/src/main/java/org/spring/springboot/webflux/controller/CityWebFluxController.java create mode 100644 springboot-webflux-9-test/src/main/resources/application.properties create mode 100644 springboot-webflux-9-test/src/test/java/org/spring/springboot/handler/CityHandlerTest.java diff --git a/springboot-webflux-9-test/pom.xml b/springboot-webflux-9-test/pom.xml new file mode 100755 index 0000000..24d9a2b --- /dev/null +++ b/springboot-webflux-9-test/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + springboot + springboot-webflux-9-test + 0.0.1-SNAPSHOT + springboot-webflux-9-test :: 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-data-mongodb-reactive + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + io.projectreactor + reactor-test + test + + + + + junit + junit + 4.12 + + + + diff --git a/springboot-webflux-9-test/src/main/java/org/spring/springboot/Application.java b/springboot-webflux-9-test/src/main/java/org/spring/springboot/Application.java new file mode 100644 index 0000000..d648b6b --- /dev/null +++ b/springboot-webflux-9-test/src/main/java/org/spring/springboot/Application.java @@ -0,0 +1,20 @@ +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-9-test/src/main/java/org/spring/springboot/dao/CityRepository.java b/springboot-webflux-9-test/src/main/java/org/spring/springboot/dao/CityRepository.java new file mode 100644 index 0000000..83e4586 --- /dev/null +++ b/springboot-webflux-9-test/src/main/java/org/spring/springboot/dao/CityRepository.java @@ -0,0 +1,10 @@ +package org.spring.springboot.dao; + +import org.spring.springboot.domain.City; +import org.springframework.data.mongodb.repository.ReactiveMongoRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CityRepository extends ReactiveMongoRepository { + +} diff --git a/springboot-webflux-9-test/src/main/java/org/spring/springboot/domain/City.java b/springboot-webflux-9-test/src/main/java/org/spring/springboot/domain/City.java new file mode 100644 index 0000000..2e9983c --- /dev/null +++ b/springboot-webflux-9-test/src/main/java/org/spring/springboot/domain/City.java @@ -0,0 +1,63 @@ +package org.spring.springboot.domain; + +import org.springframework.data.annotation.Id; + +/** + * 城市实体类 + * + */ +public class City { + + /** + * 城市编号 + */ + @Id + 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-9-test/src/main/java/org/spring/springboot/handler/CityHandler.java b/springboot-webflux-9-test/src/main/java/org/spring/springboot/handler/CityHandler.java new file mode 100644 index 0000000..096ceb8 --- /dev/null +++ b/springboot-webflux-9-test/src/main/java/org/spring/springboot/handler/CityHandler.java @@ -0,0 +1,43 @@ +package org.spring.springboot.handler; + +import org.spring.springboot.dao.CityRepository; +import org.spring.springboot.domain.City; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +@Component +public class CityHandler { + + private final CityRepository cityRepository; + + @Autowired + public CityHandler(CityRepository cityRepository) { + this.cityRepository = cityRepository; + } + + public Mono save(City city) { + return cityRepository.save(city); + } + + public Mono findCityById(Long id) { + + return cityRepository.findById(id); + } + + public Flux findAllCity() { + + return cityRepository.findAll(); + } + + public Mono modifyCity(City city) { + + return cityRepository.save(city); + } + + public Mono deleteCity(Long id) { + cityRepository.deleteById(id); + return Mono.create(cityMonoSink -> cityMonoSink.success(id)); + } +} diff --git a/springboot-webflux-9-test/src/main/java/org/spring/springboot/webflux/controller/CityWebFluxController.java b/springboot-webflux-9-test/src/main/java/org/spring/springboot/webflux/controller/CityWebFluxController.java new file mode 100644 index 0000000..3954e07 --- /dev/null +++ b/springboot-webflux-9-test/src/main/java/org/spring/springboot/webflux/controller/CityWebFluxController.java @@ -0,0 +1,41 @@ +package org.spring.springboot.webflux.controller; + +import org.spring.springboot.domain.City; +import org.spring.springboot.handler.CityHandler; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +@RestController +@RequestMapping(value = "/city") +public class CityWebFluxController { + + @Autowired + private CityHandler cityHandler; + + @GetMapping(value = "/{id}") + public Mono findCityById(@PathVariable("id") Long id) { + return cityHandler.findCityById(id); + } + + @GetMapping() + public Flux findAllCity() { + return cityHandler.findAllCity(); + } + + @PostMapping() + public Mono saveCity(@RequestBody City city) { + return cityHandler.save(city); + } + + @PutMapping() + public Mono modifyCity(@RequestBody City city) { + return cityHandler.modifyCity(city); + } + + @DeleteMapping(value = "/{id}") + public Mono deleteCity(@PathVariable("id") Long id) { + return cityHandler.deleteCity(id); + } +} diff --git a/springboot-webflux-9-test/src/main/resources/application.properties b/springboot-webflux-9-test/src/main/resources/application.properties new file mode 100644 index 0000000..270284a --- /dev/null +++ b/springboot-webflux-9-test/src/main/resources/application.properties @@ -0,0 +1,5 @@ +spring.data.mongodb.host=localhost +spring.data.mongodb.database=admin +spring.data.mongodb.port=27017 +spring.data.mongodb.username=admin +spring.data.mongodb.password=admin diff --git a/springboot-webflux-9-test/src/test/java/org/spring/springboot/handler/CityHandlerTest.java b/springboot-webflux-9-test/src/test/java/org/spring/springboot/handler/CityHandlerTest.java new file mode 100644 index 0000000..316a714 --- /dev/null +++ b/springboot-webflux-9-test/src/test/java/org/spring/springboot/handler/CityHandlerTest.java @@ -0,0 +1,52 @@ +package org.spring.springboot.handler; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.spring.springboot.domain.City; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.reactive.function.BodyInserters; + +import java.util.HashMap; +import java.util.Map; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class CityHandlerTest { + + @Autowired + private WebTestClient webClient; + + private static Map cityMap = new HashMap<>(); + + @BeforeClass + public static void setup() throws Exception { + City wl = new City(); + wl.setId(1L); + wl.setProvinceId(2L); + wl.setCityName("WL"); + wl.setDescription("WL IS GOOD"); + cityMap.put("WL", wl); + } + + @Test + public void testSave() throws Exception { + + City expectCity = webClient.post().uri("/city") + .contentType(MediaType.APPLICATION_JSON) + .body(BodyInserters.fromObject(cityMap.get("WL"))) + .exchange() + .expectStatus().isOk() + .expectBody(City.class).returnResult().getResponseBody(); + + Assert.assertNotNull(expectCity); + Assert.assertEquals(expectCity.getId(), cityMap.get("WL").getId()); + Assert.assertEquals(expectCity.getCityName(), cityMap.get("WL").getCityName()); + } + +}