The functional model of WebFlux is quite different from the annotation based model. Where the annotation based model builds upon the already familiar @Controller,@RequestMapping and other web annotations, the functional model sets these aside and introduces concept of a RouterFunction and HandlerFunction.

RouterFunction

Router functions are the functional counterpart of the @RequestMapping annotation and used to match incomming requests to handler functions. A router function takes a ServletRequest as input and returns the appropriate handler function. The signature of a router function is as follows:

1

2

Mono<HandlerFunction<T>>route(ServerRequest request);

The router function definition for the @RequestMapping(path={"/planets/{substring}","/planets"}) annotation looks like this:

1

2

3

4

5

6

7

8

9

10

11

12

13

@Configuration

@AllArgsConstructor

publicclassPlanetRoutes{

privatePlanetHandler planetHandler;

@Bean

publicRouterFunction<?>planetRouter(){

returnroute(GET("/planets")

.or(GET("/planets/{substring}")),planetHandler::getPlanets);

}

}

HandlerFunction

Handler functions contain the actual business logic for processing the request. The signature of a handler function is as follows:

1

2

Mono<ServerResponse>handle(ServerRequest request);

The handler function implementation does not differ that much from the controller implementation, except that we now have to deal with a ServerRequest object that is our starting point for processing the request. The ServerRequest offers us reactive style access to request information. Transforming the controller logic to handler logic gives the following: