从浏览器/REST客户端调用时如何自动预订REST端点? [英] How REST endpoints are auto subscribed while calling from Browser/REST Client?

查看:88
本文介绍了从浏览器/REST客户端调用时如何自动预订REST端点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ProjectReactor或反应式流中,在您订阅()之前,什么都不会发生.

In ProjectReactor or Reactive Streams, Nothing Happens Until You subscribe().

除非有人进行订阅,否则不会发生反应流数据流,但是我发现对于所有REST API,例如find,save和inserts都不会显式调用订阅,而是数据在生产者和订阅者之间流动.

Reactive streams data flow will not happen unless until someone subscribe to it, but I see for all REST APIs like finds, save and inserts are not calling subscribe explicitly but data is flowing between producer and subscribers.

@RestController
class PersonController {

      private final PersonRepository repository;

      public PersonController(PersonRepository repository) {
        this.repository = repository;
      }
      @GetMapping("/all")
      public Flux<Person> index() {

         return repository.findAll();

     }
      @GetMapping("/people")
      Flux<String> namesByLastname(@RequestParam Mono<String> lastname) {

        Flux<Person> result = repository.findByLastname(lastname);
        return result.map(it -> it.getFullName());
      }

      @PostMapping("/people")
      Flux<People> AddPeople(@RequestBody Flux<Person> people) {

          return repository.saveAll(people);
      }
}

为什么我们无需为REST端点调用订阅即可在Project Reactor中启动数据流?

why do we no need to call subscribe for REST Endpoints to start a data flow in Project Reactor?

当我从浏览器中调用时,REST端点(HTTP请求)如何自动订阅响应流以进行数据流?

How REST endpoints (HTTP requests) are auto-subscribing to Reactive Streams for data flow when i call from browser?

我在这里想念东西吗?

推荐答案

您是对的-当您的应用程序设置Flux/Mono反应性管道时,该管道中的任何内容都不会执行,直到subscribe

You're right - when your application is setting up a Flux/Mono reactive pipeline, nothing in that pipeline is executed until something subscribe to it.

这是Spring WebFlux中的请求/响应交换期间发生的事情:

Here's what's happening during a request/response exchange in Spring WebFlux:

  • 服务器收到请求并将其转发到WebFlux
  • 根据请求和您的应用程序代码,将构建一个反应性管道,其中涉及过滤器,控制器等.您可以将其视为将请求链接到响应的管道
  • HTTP客户端通过TCP堆栈请求读取,并且背压信息由基础服务器传输.

Spring WebFlux中最低的合同是 ServletHttpHandlerAdapter ,我们在反应流世界和异步I/O Servlet API之间架起了桥梁-订阅实际上发生在该网桥之内.

The lowest contract in Spring WebFlux is HttpHandler - it's the contract that interfaces with the underlying server. In the case of Reactor Netty, this server already supports the reactive streams API and the subscription is natively done by the server. For other Servlet-based servers, we're using a reactive streams bridge to Servlet 3.1. In ServletHttpHandlerAdapter, we're bridging between the reactive streams world and the async I/O Servlet API - the subscription actually happens within that bridge.

也:请注意,我们通常不subscribe使用WebClient返回的值.仅当您不在反应式管道的中间(即不在Controller处理程序的中间)时,才可以这样做.在这种情况下,我们通常将其插入管道中间的反应式运算符中;如果不这样做,就无法保证将何时获得HTTP客户端响应-这将使该调用与应用程序的其余部分完全分离.

Also: note that we don't usually subscribe to the value returned by WebClient; you can only do that if you're not in the middle of a reactive pipeline (i.e. not in the middle of a Controller handler). In those cases, we usually plug that into a reactive operator in the middle of the pipeline; if you don't, you'll have no guarantee whatsoever about when you'll get the HTTP client response - this totally decouples that call from the rest of your application.

这篇关于从浏览器/REST客户端调用时如何自动预订REST端点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆