javascript
spring react_使用Spring WebFlux构建React性REST API –第3部分
spring react
在上一篇文章的續篇中,我們將看到一個應用程序以公開React性REST API。 在此應用程序中,我們使用了
- 帶有WebFlux的Spring Boot
- 具有響應式支持的Cassandra的Spring數據
- 卡桑德拉數據庫
下面是應用程序的高級體系結構。
讓我們看一下build.gradle文件,以查看與Spring WebFlux一起使用的依賴項。
plugins {id 'org.springframework.boot' version '2.2.6.RELEASE'id 'io.spring.dependency-management' version '1.0.9.RELEASE'id 'java' }group = 'org.smarttechie' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8'repositories {mavenCentral() }dependencies {implementation 'org.springframework.boot:spring-boot-starter-data-cassandra-reactive'implementation 'org.springframework.boot:spring-boot-starter-webflux'testImplementation('org.springframework.boot:spring-boot-starter-test') {exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'}testImplementation 'io.projectreactor:reactor-test' }test {useJUnitPlatform() }在此應用程序中,我公開了以下提到的API。 您可以從GitHub下載源代碼。
| 終點 | URI | 響應 |
| 創建產品 | /產品 | 創建產品為Mono |
| 所有產品 | /產品 | 以Flux的形式返回所有產品 |
| 發售產品 | / product / {id} | 單核細胞增多癥 |
| 更新產品 | / product / {id} | 將產品更新為Mono |
具有上述所有端點的產品控制器代碼如下。
package org.smarttechie.controller;import org.smarttechie.model.Product; import org.smarttechie.repository.ProductRepository; import org.smarttechie.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono;@RestController public class ProductController {@Autowiredprivate ProductService productService;/*** This endpoint allows to create a product.* @param product - to create* @return - the created product*/@PostMapping("/product")@ResponseStatus(HttpStatus.CREATED)public Mono<Product> createProduct(@RequestBody Product product){return productService.save(product);}/*** This endpoint gives all the products* @return - the list of products available*/@GetMapping("/products")public Flux<Product> getAllProducts(){return productService.getAllProducts();}/*** This endpoint allows to delete a product* @param id - to delete* @return*/@DeleteMapping("/product/{id}")public Mono<Void> deleteProduct(@PathVariable int id){return productService.deleteProduct(id);}/*** This endpoint allows to update a product* @param product - to update* @return - the updated product*/@PutMapping("product/{id}")public Mono<ResponseEntity<Product>> updateProduct(@RequestBody Product product){return productService.update(product);} }在構建React式API時,我們可以使用功能樣式編程模型來構建API,而無需使用RestController。 在這種情況下,我們需要具有一個路由器和一個處理程序組件,如下所示。
package org.smarttechie.router;import org.smarttechie.handler.ProductHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; 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 static org.springframework.web.reactive.function.server.RequestPredicates.*; @Configuration public class ProductRouter {/*** The router configuration for the product handler.* @param productHandler* @return*/@Beanpublic RouterFunction<ServerResponse> productsRoute(ProductHandler productHandler){return RouterFunctions.route(GET("/products").and(accept(MediaType.APPLICATION_JSON)),productHandler::getAllProducts).andRoute(POST("/product").and(accept(MediaType.APPLICATION_JSON)),productHandler::createProduct).andRoute(DELETE("/product/{id}").and(accept(MediaType.APPLICATION_JSON)),productHandler::deleteProduct).andRoute(PUT("/product/{id}").and(accept(MediaType.APPLICATION_JSON)),productHandler::updateProduct);} }package org.smarttechie.handler;import org.smarttechie.model.Product; import org.smarttechie.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerResponse; import reactor.core.publisher.Mono; import static org.springframework.web.reactive.function.BodyInserters.fromObject;@Component public class ProductHandler {@Autowiredprivate ProductService productService;static Mono<ServerResponse> notFound = ServerResponse.notFound().build();/*** The handler to get all the available products.* @param serverRequest* @return - all the products info as part of ServerResponse*/public Mono<ServerResponse> getAllProducts(ServerRequest serverRequest) {return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(productService.getAllProducts(), Product.class);}/*** The handler to create a product* @param serverRequest* @return - return the created product as part of ServerResponse*/public Mono<ServerResponse> createProduct(ServerRequest serverRequest) {Mono<Product> productToSave = serverRequest.bodyToMono(Product.class);return productToSave.flatMap(product ->ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(productService.save(product), Product.class));}/*** The handler to delete a product based on the product id.* @param serverRequest* @return - return the deleted product as part of ServerResponse*/public Mono<ServerResponse> deleteProduct(ServerRequest serverRequest) {String id = serverRequest.pathVariable("id");Mono<Void> deleteItem = productService.deleteProduct(Integer.parseInt(id));return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(deleteItem, Void.class);}/*** The handler to update a product.* @param serverRequest* @return - The updated product as part of ServerResponse*/public Mono<ServerResponse> updateProduct(ServerRequest serverRequest) {return productService.update(serverRequest.bodyToMono(Product.class)).flatMap(product ->ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(fromObject(product))).switchIfEmpty(notFound);} }到目前為止,我們已經看到了如何公開響應式REST API。 通過此實現,我已經使用Gatling在React式API和非React式API(使用Spring RestController構建非React式API)上做了一個簡單的基準測試。 以下是React式和非React式API之間的比較指標。 這不是一個廣泛的基準測試。 因此,在采用之前,請確保對您的用例進行廣泛的基準測試。
GitHub上也提供了Gatling負載測試腳本,供您參考。 到此,我結束了“ 用Spring WebFlux構建響應式REST API ”系列。 我們將在另一個主題上見面。 到那時, 快樂學習!!
翻譯自: https://www.javacodegeeks.com/2020/06/build-reactive-rest-apis-with-spring-webflux-part3.html
spring react
總結
以上是生活随笔為你收集整理的spring react_使用Spring WebFlux构建React性REST API –第3部分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安啦安啦是什么意思 安啦安啦的意思是什么
- 下一篇: spring react_使用Sprin