How REST endpoints are auto subscribed while calling from Browser/REST Client?
Asked Answered
E

1

10

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

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);
      }
}

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

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

am i missing something here ?

Edmonds answered 11/6, 2018 at 10:0 Comment(5)
That's how reactive streams work. You need to subscribe. It's a bit like saying why do I need a connection to my database to run a query.Gramme
@Gramme His question is a bit different. What he is asking is(at least I believe so) when i call a rest endpoint through java code with Webclient i need to use subscribe(), but when i call these rest endpoint from a browser, how is it auto subscribed. This is a fair question i wud say.Rotterdam
@pvpkiran, you are correct. let me alter the question.Edmonds
@PrabuSubra spring web-flux works over the http server, if you use spring-boot, by default it will be http server "Netty". Http server handles incoming http request and subscribes on the concrete publisher.Braddy
Related: stackoverflow.com/questions/56487429Appetizer
G
6

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.

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

  • the server receives a request and forwards that to WebFlux
  • depending on the request and your application code, a reactive pipeline will be built, involving filters, controllers, etc. You could see that as a pipe linking the request to the response
  • the HTTP client, through the TCP stack, requests reads and that backpressure information is transmitted by the underlying server.

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.

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.

Georgetown answered 11/6, 2018 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.