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:
- Added @RestController to the ReactiveController class. This enables the ...