Skip to content
Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

WebFlux with Kotlin by Example

A set of examples on how to use Spring’s WebFlux framework with Kotlin, with an emphasis on reactive and asynchronous aspects.

Bootstrapping a project

Go to, and select:

  • Project: Gradle project

  • Language: Kotlin

  • Spring Boot: most recent stable release

  • Packaging: Jar

  • Java: 11

  • Dependencies: Spring Reactive Web

When selecting "Generate", a zip archive is created containing the project basic structure:

  • gradle folder, gradlew file, and gradlew.bat file, containing the gradle wrapper and bootstrapper for a given version (e.g. 6.0.1).

  • build.gradle.kts file with the Gradle build script. Notice that this build script uses the Kotlin language and not the Gradle language. This choice was done by the project generator because the selected language was Kotlin. All the required dependencies needed are defined inside this file.

  • settings.gradle.kts file with additional Gradle settings.

  • src folder containing two source files, one in main and another in test:

  • A file with the application entry point.

  • A test file with a simple test that confirms the Spring context can be correctly initialized.

Application entry point

Notice that the application entry point is a global kotlin function.

fun main(args: Array<String>) {

Also, runApplication is a top level function provided by Spring Boot (source) that takes advantage of reified generics to avoid passing a Class object.

Spring Boot requires a correctly annotated application class to be passed to runApplication, so an empty class is used for that purpose.

class App

This class can be used to contain bean definitions.


HttpHandler seems to be the lowest common abstraction around the handling * of an HTTP exchange, playing a role similar to servlets in the old Spring MVC. Netty can be used to directly host an HttpHandler, without requiring Spring Boot or a Spring context.

See UsingHttpHandler.kt for an example of how to create an HttpHandler and host it.


A WebHandler differs from an HttpHandler in the following ways:

  • It is called at the end of a pipeline comprised of WebFilter and WebExceptionHandler instances.

  • Has access to a richer context, represented by ServerWebExchange, including:

    • form data;

    • request principal;

    • exchange attributes set by filters.

This pipeline is built using a WebHttpHandlerBuilder and implements the HttpHandler interface, meaning that it can be hosted like any other HttpHandler.

See UsingWebHandler.kt for an example of how to create and host a pipeline with a WebHandler, filters, and exception handlers.

Router functions

Router function allows the declarative and nested definition of handling functions associated to paths or other predicates.

router {
    path("/examples").nest {
        GET("/") { req ->
  "Request received: method={}, URI={}", req.method(), req.uri())
            ok().bodyValue("Hello world, from /examples")
        GET("/{id}") { req ->
            val id = req.pathVariable("id")
  "Request received: method={}, URI={}, id={}",
                    req.method(), req.uri(), id)
            ok().bodyValue("Hello world, from /examples/{id}, with id=$id")

The outer result is a RouterFunction which is an interface with a Mono<HandlerFunction<T>> route(ServerRequest request), i.e, it is something that given a request, asynchronously returns an handler function.

Handling functions are functions from ServerRequest to Mono<ServerResponse>, i.e., (ServerRequest) → Mono<out ServerResponse>.

It is also possible to include filters into this handling tree, that wrap the calls to the handlers.

WebFlux also supports suspend functions, allowing handlers and filters to be written using Kotlin’s coroutine style, instead of using reactive streams. In this case, an handler has the following signature: suspend (ServerRequest) → ServerResponse.

See UsingRouterFunction.kt for an example.


Spring WebFlux with Kotlin by example







No releases published


No packages published