Spring Boot RouterFunction
last modified July 29, 2023
In this article we show how to create functional routes in Spring Boot applications.
Reactive programming
Reactive programming is a programming paradigm that is functional, event-based, non-blocking, asynchronous, and centered around data stream processing. The term reactive comes from the fact that we react to changes such as mouse clicks or I/O events.
Traditional Spring MVC applications use annotations such as
@GetMapping
to map request paths to controller actions. Functional
routing API is an alternative way of this mapping.
RouterFunction
RouterFunction
represents a function that routes to
a handler function.
Spring Boot RouterFunction example
In the following application we create a reactive Spring Boot application with functional routes.
build.gradle ... src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ └───routes │ │ MyRoutes.java │ └───resources └───test └───java └───com └───zetcode └───routes MyRoutesTest.java
This is the project structure of the Spring application.
plugins { id 'org.springframework.boot' version '3.1.1' id 'io.spring.dependency-management' version '1.1.0' id 'java' } group = 'com.zetcode' version = '0.0.1-SNAPSHOT' sourceCompatibility = '17' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-webflux' testImplementation 'org.springframework.boot:spring-boot-starter-test' } test { useJUnitPlatform() }
This is the build.gradle
file. The RouterFunction
is in the spring-boot-starter-webflux
dependency.
package com.zetcode.routes; 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 static org.springframework.web.reactive.function.BodyInserters.fromValue; import static org.springframework.web.reactive.function.server.RequestPredicates.GET; import static org.springframework.web.reactive.function.server.RouterFunctions.route; import static org.springframework.web.reactive.function.server.ServerResponse.ok; @Configuration public class MyRoutes { @Bean RouterFunction<ServerResponse> home() { return route(GET("/"), request -> ok().body(fromValue("Home page"))); } @Bean RouterFunction<ServerResponse> about() { return route(GET("/about"), request -> ok().body(fromValue("About page"))); } }
We define two function routes.
@Bean RouterFunction<ServerResponse> home() { return route(GET("/"), request -> ok().body(fromValue("Home page"))); }
With functional routes, we can write simple and elegant code. Here we return a simple text message for the home page.
package com.zetcode.routes; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.reactive.server.WebTestClient; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class MyRoutesTest { @Autowired private WebTestClient client; @Test public void test_home_page() { client.get().uri("/").exchange().expectStatus().isOk() .expectBody(String.class).isEqualTo("Home page"); } @Test public void test_about_page() { client.get().uri("/about").exchange().expectStatus().isOk() .expectBody(String.class).isEqualTo("About page"); } }
With WebTestClient
, we test the two routes.
package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
This code sets up the Spring Boot application.
$ ./gradlew bootRun
We run the application and navigate to localhost:8080
.
In this article we have learned how to use functional routes with
RouterFunction
.