Adding a controller

We need to add a controller that can serve Fibonacci numbers. As discussed in the preceding sections, we need to add a class with the @RestController annotation. Let's look at the following controller: 

@RestControllerpublic class ReactiveController { @GetMapping("/fibonacci") @ResponseBody public Publisher<Long>fibonacciSeries() { Flux<Long> fibonacciGenerator = Flux.generate(() -> Tuples.<Long,   Long>of(0L, 1L), (state, sink) -> {   if (state.getT1() < 0)   sink.complete();   else   sink.next(state.getT1());   return Tuples.of(state.getT2(), state.getT1() + state.getT2()); }); return fibonacciGenerator; }}

In the preceding class, we have done the following:

  1. Added @RestController to the ReactiveController class. This enables the ...

Get Hands-On Reactive Programming with Reactor 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.