Creating a Reactive Controller

Creating a Spring Reactive Controller is very similar to creating a Spring MVC Controller. The basic constructs are the same: @RestController and the different @RequestMapping annotations. The following snippet shows a simple reactive controller named StockPriceEventController:

    @RestController    public class StockPriceEventController {      @GetMapping("/stocks/price/{stockCode}")      Flux<String> retrieveStockPriceHardcoded      (@PathVariable("stockCode") String stockCode) {        return Flux.interval(Duration.ofSeconds(5))        .map(l -> getCurrentDate() + " : "         + getRandomNumber(100, 125))        .log();      }     private String getCurrentDate() {       return (new Date()).toString();     }     private int getRandomNumber(int min, int max) { return ThreadLocalRandom.current().nextInt(min, ...

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.