Spring WebFlux provides a lightweight, functional programming model where functions
are used to route and handle requests and where contracts are designed for immutability.
It is an alternative to the annotated-based programming model but runs on the same
<<web-reactive.adoc#webflux-reactive-spring-web>> foundation
Spring WebFlux includes a lightweight, functional programming model in which functions
are used to route and handle requests and contracts are designed for immutability.
It is an alternative to the annotated-based programming model but otherwise running on
the same <<web-reactive.adoc#webflux-reactive-spring-web>> foundation
@ -13,19 +13,19 @@ It is an alternative to the annotated-based programming model but runs on the sa
@@ -13,19 +13,19 @@ It is an alternative to the annotated-based programming model but runs on the sa
== HandlerFunction
Incoming HTTP requests are handled by a **`HandlerFunction`**, which is essentially a function that
takes a `ServerRequest` and returns a `Mono<ServerResponse>`. The annotation counterpart to a
handler function is an `@RequestMapping` method.
takes a `ServerRequest` and returns a `Mono<ServerResponse>`. If you're familiar with the
annotation-based programming model, a handler function is the equivalent of an
`@RequestMapping` method.
`ServerRequest` and `ServerResponse` are immutable interfaces that offer JDK-8 friendly access
to the underlying HTTP messages with http://www.reactive-streams.org[Reactive Streams]
non-blocking back pressure. The request exposes the body as Reactor `Flux` or `Mono`
types; the response accepts any Reactive Streams `Publisher` as body (see
@ -129,8 +129,9 @@ found. If it is not found, we use `switchIfEmpty(Mono<T>)` to return a 404 Not F
@@ -129,8 +129,9 @@ found. If it is not found, we use `switchIfEmpty(Mono<T>)` to return a 404 Not F
Incoming requests are routed to handler functions with a **`RouterFunction`**, which is a function
that takes a `ServerRequest`, and returns a `Mono<HandlerFunction>`. If a request matches a
particular route, a handler function is returned; otherwise it returns an empty `Mono`. The
`RouterFunction` has a similar purpose as the `@RequestMapping` annotation in `@Controller` classes.
particular route, a handler function is returned, or otherwise an empty `Mono` is returned.
`RouterFunction` has a similar purpose as the `@RequestMapping` annotation in the
annotation-based programming model.
Typically, you do not write router functions yourself, but rather use
`RouterFunctions.route(RequestPredicate, HandlerFunction)` to
@ -192,17 +193,71 @@ For instance, `RequestPredicates.GET(String)` is a composition of
@@ -192,17 +193,71 @@ For instance, `RequestPredicates.GET(String)` is a composition of
[[webflux-fn-running]]
== Running a server
How do you run a router function in an HTTP server? A simple option is to convert a
router function to an `HttpHandler` via `RouterFunctions.toHttpHandler(RouterFunction)`.
The `HttpHandler` can then be used with a number of servers adapters.
See <<web-reactive.adoc#webflux-httphandler,HttpHandler>> for server-specific
instructions.
it is also possible to run with a
<<web-reactive.adoc#webflux-dispatcher-handler,DispatcherHandler>> setup -- side by side
with annotated controllers. The easiest way to do that is through the
<<web-reactive.adoc#webflux-config>> which creates the necessary configuration to
handle requests with router and handler functions.
How do you run a router function in an HTTP server? A simple option is to convert a router
function to an `HttpHandler` using one of the following: