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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/331177.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

tomcat(12)org.apache.catalina.core.StandardContext源码剖析

【0】README0&#xff09;本文部分文字描述转自 “how tomcat works”&#xff0c;旨在学习 “tomcat(12)StandardContext源码剖析” 的基础知识&#xff1b;1&#xff09;Context实例表示一个具体的web 应用程序&#xff0c;其中包含一个或多个Wrapper实例&#xff0c;每个Wra…

jms面试题_最新的20多个JMS面试问答(2020)

jms面试题快速浏览JMS&#xff08;Java消息服务&#xff09;常见问题面试问题和答案。 2020年以来的最新问题已添加到列表中。 1.简介 在本教程中&#xff0c;我们将了解2020年经常问到的JMS面试问题。作为面试的一部分&#xff0c;如果您有6年以上的经验&#xff0c;就有机会…

tomcat(13)Host和Engine容器

【0】README0.1&#xff09;本文部分文字描述转自 “how tomcat works”&#xff0c;旨在学习 “tomcat(13)Host和Engine容器” 的基础知识&#xff1b;0.2&#xff09;Host容器&#xff1a;若你想在同一个tomcat部署 上运行多个Context 容器的话&#xff0c;就需要使用 Host 容…

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

spring react在上一篇文章的续篇中&#xff0c;我们将看到React式流规范及其实现之一&#xff0c;称为Project Reactor 。 React流规范定义了以下接口。 让我们看看这些接口的细节。 发布者→发布者是潜在数量不受限制的序列元素的提供者&#xff0c;可按其订阅者的要求发布它…

tomcat(14)服务器组件和服务组件

【0】README0.1&#xff09;本文部分文字描述转自 “how tomcat works”&#xff0c;旨在学习 “tomcat(14)服务器组件和服务组件” 的基础知识&#xff1b;0.2&#xff09;for complete source code ,please visit https://github.com/pacosonTang/HowTomcatWorks/tree/maste…

组合模式的安全模式与透明模式

转载自 树形结构的处理——组合模式&#xff08;四&#xff09; 1 透明组合模式与安全组合模式 通过引入组合模式&#xff0c;Sunny公司设计的杀毒软件具有良好的可扩展性&#xff0c;在增加新的文件类型时&#xff0c;无须修改现有类库代码&#xff0c;只需增加一个新的文件类…

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

spring react在本文中&#xff0c;我们将看到如何使用Spring WebFlux构建响应式REST API。 在进入React式API之前&#xff0c;让我们看看系统是如何发展的&#xff0c;传统REST实现遇到的问题以及现代API的需求。 如果您查看从旧版系统到下文所述的现代系统的期望&#xff0c;…

tomcat(15)Digester库

【0】README0.1&#xff09;本文部分文字描述转自 “how tomcat works”&#xff0c;旨在学习 “tomcat(15)Digester库” 的基础知识&#xff1b;2&#xff09;problemsolution&#xff1a;2.1&#xff09;problem&#xff1a;如下面的代码&#xff0c;Bootstrap类实例化连接器…

api restful_HATEOAS的RESTful服务。 记录超媒体API

api restful1.简介 希望本教程的前一部分不仅揭示了超媒体和HATEOAS的深远意义&#xff0c;而且使我们确信这些都是RESTful Web服务和API的基本构建模块。 在这一部分中&#xff0c;我们将继续侧重于文档方面&#xff0c;以解决如何预先传递Web服务或API功能的问题。 目录 1.…

模版方法模式

转载自 23种设计模式&#xff08;6&#xff09;&#xff1a;模版方法模式定义&#xff1a;定义一个操作中算法的框架&#xff0c;而将一些步骤延迟到子类中&#xff0c;使得子类可以不改变算法的结构即可重定义该算法中的某些特定步骤。 类型&#xff1a;行为类模式 类图&#…

java并发编程实践(1)intro

【0】README0.1&#xff09;本文部分文字描述转自“java并发编程实践”&#xff0c;旨在学习“java并发编程实践(1)intro”的相关知识&#xff1b;【3】线程带来的风险【3.1】安全性问题1&#xff09;intro&#xff1a;在没有充足同步的case下&#xff0c;多个线程的操作执行顺…

读入的字节都写入字节数组中_使用Java将文件读入字节数组的7个示例

读入的字节都写入字节数组中嗨&#xff0c;大家好&#xff0c;Java程序员经常在现实世界中遇到编程问题&#xff0c;他们需要将文件中的数据加载到字节数组中&#xff0c;该文件可以是文本文件或二进制文件。 一个示例是将文件的内容转换为String以显示。 不幸的是&#xff0c;…

迭代子模式

转载自 《JAVA与模式》之迭代子模式 在阎宏博士的《JAVA与模式》一书中开头是这样描述迭代子&#xff08;Iterator&#xff09;模式的&#xff1a; 迭代子模式又叫游标(Cursor)模式&#xff0c;是对象的行为模式。迭代子模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部…

java并发编程实践(2)线程安全性

【0】README 0.0&#xff09;本文部分文字描述转自&#xff1a;“java并发编程实战”&#xff0c; 旨在学习“java并发编程实践(2)线程安全性” 的相关知识&#xff1b;0.1&#xff09;几个术语&#xff08;terms&#xff09;t1&#xff09;对象的状态&#xff1a;是指存储在状…

自动配置jdk_JDK 15中自动自动发送更好的NullPointerException消息

自动配置jdk我讨论了期待已久的和高度赞赏改进的NullPointerException &#xff08; NPE在帖&#xff09;消息“ 更好的默认NullPointerException异常消息即将到Java&#xff1f; ”和“ JDK 14中更好的NPE消息 ”。 当此JEP 358驱动的功能添加到JDK 14中时 &#xff0c;想要从…

tomat(16)关闭钩子

【0】REAMDE0&#xff09;本文部分文字描述转自&#xff1a;“how tomcat works”&#xff0c;旨在学习“tomat(16)关闭钩子”的相关知识&#xff1b;1&#xff09;problemsolution&#xff1a;1.1&#xff09;problem&#xff1a;在很多实际环境中&#xff0c;当用户关闭应用程…

apache.camel_Apache Camel 3.1 – XML路由的快速加载

apache.camelCamel 3.1中添加的一项功能是能够更快地加载XML路由。 这是我们为使Camel变得更小&#xff0c;更快而进行的总体工作的一部分。 您可能会说ewww XML。 但坦率地说&#xff0c;有很多Camel用户使用XML定义路由来构建应用程序。 在Camel 2.x中&#xff0c;您将不得不…

程序中 设置jvm 参数_高效应用程序的7个JVM参数

程序中 设置jvm 参数在撰写本文时&#xff08;2020年3月&#xff09;&#xff0c;围绕垃圾收集和内存&#xff0c;您可以将600多个参数传递给JVM。 如果您包括其他方面&#xff0c;则JVM参数总数将很容易超过1000个。 &#x1f60a;。 任何人都无法消化和理解太多的论据。 在本…

tomcat(17)启动tomcat

【0】README 1&#xff09;本文部分文字描述转自“how tomcat works”&#xff0c;旨在学习“tomcat(17)启动tomcat”的相关知识&#xff1b;2&#xff09;本文重点关注启动Tomcat时会用到的两个类&#xff0c;分别是Catalina类和 Bootstrap类&#xff1b;&#xff08;干货——…

八爪鱼 是java做的吗_章鱼扫描仪:Java构建工具和恶意软件

八爪鱼 是java做的吗Alvaro Munoz最近在GitHub Security Lab网站上发布了“ Octopus扫描程序恶意软件&#xff1a;攻击开源供应链 ”。 我发现这篇文章很有趣&#xff0c;原因有很多&#xff0c;其中包括对Octopus Scanner恶意软件的工作原理和发现方式的详细介绍&#xff0c; …