Spring Webflux Router

This post walks you through the process of creating endpoints using Spring Webflux routers. Please read this previous post before conitnue with this information.

In Spring MVC we are using @Controller in WebFlux we are using:

Router Function

Incoming requests are routed to handler functions with a RouterFunction<T> it has a similar purpose as a @RequestMapping annotation.

Let’s consider the following example:

package com.jos.dem.webflux.config;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;

import com.jos.dem.webflux.model.Person;
import com.jos.dem.webflux.repository.PersonRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.function.server.RouterFunctions;

public class WebConfig {

  private PersonRepository personRepository;

  public RouterFunction<ServerResponse> routes(){
    return RouterFunctions
      request -> ServerResponse.ok().body(personRepository.findAll(), Person.class))
      request -> ServerResponse.ok().body(personRepository.findById(request.pathVariable("id")), Person.class));


The original Spring Web MVC was running on Tomcat and was purpose built for the Servlet API and Servlet containers. The reactive web framework Spring WebFlux is fully non-blocking, supports Reactive Streams and runs on Netty server, but at the end from the client perspective we hit them in the same way.

So, after adding this configuration class to your project and run Spring Boot, you can go to: http://localhost:8080/persons

    "uuid": "7baac840-0ff1-4eea-a1f3-78a6320f5bcc",
    "nickname": "edzero",
    "email": "edzero@email.com"
    "uuid": "7b098510-e260-4dc2-8b15-24fc403eb939",
    "nickname": "skuarch",
    "email": "skuarch@email.com"
    "uuid": "d6089e68-e8c4-4ebe-aa2f-b566e13856a8",
    "nickname": "siedrix",
    "email": "siedrix@email.com"
    "uuid": "ac55dfb8-795d-48d9-9827-4e401c0e4853",
    "nickname": "tgrip",
    "email": "tgrip@email.com"
    "uuid": "5904ff32-7e36-4a81-b90f-c0d7a91214eb",
    "nickname": "josdem",
    "email": "josdem@email.com"

And this one is the endpoint to get a person by id: http://localhost:8080/persons/5904ff32-7e36-4a81-b90f-c0d7a91214eb

  "uuid": "5904ff32-7e36-4a81-b90f-c0d7a91214eb",
  "nickname": "josdem",
  "email": "josdem@email.com"


Let me conclude by giving a short summary:

  • Spring Boot now embraces reactive programming
  • Router functions route to handler functions
  • Router functions can be run in a reactive web runtime
  • WebFlux combines well with Java functional programming
  • WebFlux runs on Netty server by default

To browse the project go here, to download the project:

git clone https://github.com/josdem/reactive-webflux-workshop.git
git fetch
git checkout feature/router

To run the project:

gradle bootRun

Return to the main article

comments powered by Disqus