Creating Reactive methods in Rest Controller

We can now add in the controller methods to retrieve details using StockMongoReactiveCrudRepository:

    @RestController
public class StockPriceEventController {
private final StockMongoReactiveCrudRepository repository;
public StockPriceEventController(
StockMongoReactiveCrudRepository repository) {
this.repository = repository;
}

@GetMapping("/stocks")
Flux<Stock> list() {
return this.repository.findAll().log();
}

@GetMapping("/stocks/{code}")
Mono<Stock> findById(@PathVariable("code") String code) {
return this.repository.findById(code).log();
}
}

A few important things to note are as follows:

  • private final StockMongoReactiveCrudRepository repository: StockMongoReactiveCrudRepository is injected in using the constructor injection.
  • @GetMapping("/stocks") Flux<Stock> list(): Exposes a GET method to retrieve a list of stocks. Returns a Flux indicating that this would be a stream of stocks.
  • @GetMapping("/stocks/{code}") Mono<Stock> findById(@PathVariable("code") String code): findById returns a Mono, indicating that it would return 0 or 1 stock element(s).
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset