javascript
spring react_使用Spring WebFlux构建React性REST API –第3部分
spring react
在上一篇文章的續(xù)篇中,我們將看到一個(gè)應(yīng)用程序以公開(kāi)React性REST API。 在此應(yīng)用程序中,我們使用了
- 帶有WebFlux的Spring Boot
- 具有響應(yīng)式支持的Cassandra的Spring數(shù)據(jù)
- 卡桑德拉數(shù)據(jù)庫(kù)
下面是應(yīng)用程序的高級(jí)體系結(jié)構(gòu)。
讓我們看一下build.gradle文件,以查看與Spring WebFlux一起使用的依賴項(xiàng)。
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() }在此應(yīng)用程序中,我公開(kāi)了以下提到的API。 您可以從GitHub下載源代碼。
| 終點(diǎn) | URI | 響應(yīng) |
| 創(chuàng)建產(chǎn)品 | /產(chǎn)品 | 創(chuàng)建產(chǎn)品為Mono |
| 所有產(chǎn)品 | /產(chǎn)品 | 以Flux的形式返回所有產(chǎn)品 |
| 發(fā)售產(chǎn)品 | / product / {id} | 單核細(xì)胞增多癥 |
| 更新產(chǎn)品 | / product / {id} | 將產(chǎn)品更新為Mono |
具有上述所有端點(diǎn)的產(chǎn)品控制器代碼如下。
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);} }在構(gòu)建React式API時(shí),我們可以使用功能樣式編程模型來(lái)構(gòu)建API,而無(wú)需使用RestController。 在這種情況下,我們需要具有一個(gè)路由器和一個(gè)處理程序組件,如下所示。
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);} }到目前為止,我們已經(jīng)看到了如何公開(kāi)響應(yīng)式REST API。 通過(guò)此實(shí)現(xiàn),我已經(jīng)使用Gatling在React式API和非React式API(使用Spring RestController構(gòu)建非React式API)上做了一個(gè)簡(jiǎn)單的基準(zhǔn)測(cè)試。 以下是React式和非React式API之間的比較指標(biāo)。 這不是一個(gè)廣泛的基準(zhǔn)測(cè)試。 因此,在采用之前,請(qǐng)確保對(duì)您的用例進(jìn)行廣泛的基準(zhǔn)測(cè)試。
GitHub上也提供了Gatling負(fù)載測(cè)試腳本,供您參考。 到此,我結(jié)束了“ 用Spring WebFlux構(gòu)建響應(yīng)式REST API ”系列。 我們將在另一個(gè)主題上見(jiàn)面。 到那時(shí), 快樂(lè)學(xué)習(xí)!!
翻譯自: https://www.javacodegeeks.com/2020/06/build-reactive-rest-apis-with-spring-webflux-part3.html
spring react
總結(jié)
以上是生活随笔為你收集整理的spring react_使用Spring WebFlux构建React性REST API –第3部分的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 安啦安啦是什么意思 安啦安啦的意思是什么
- 下一篇: spring react_使用Sprin