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 using the constructor injection.
- @GetMapping("/stocks") Flux<Stock> list(): Exposes a GET method to retrieve a list of stocks. It returns a Flux indicating that this will be a stream of stocks.
- @GetMapping("/stocks/{code}") Mono<Stock> findById(@PathVariable("code") String code): findById returns a Mono, indicating that it will return 0 or 1 stock element(s).