日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

spring react_使用Spring WebFlux构建React性REST API –第3部分

發布時間:2023/12/3 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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部分的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。