Mastering Spring 5 Functional Endpoints: A Hands‑On Guide
This article shows how to replace traditional Spring MVC annotations with Spring 5's functional routing API, covering dependencies, RouterFunction, ServerRequest/ServerResponse, HandlerFunction, RequestPredicate, and demonstrating practical code examples and expected responses.
1. Introduction
Typical Spring MVC controller code uses @RestController and @RequestMapping . This article demonstrates a different style by using the functional endpoints introduced in Spring 5, which also work with Spring WebFlux.
@RestController
@RequestMapping("/v1/userinfo")
public class UserInfoController {
@GetMapping("/foo")
public String foo() {
return "felord.cn";
}
}Instead of the annotation‑based approach, we use Spring 5’s functional routing API.
Note: The functional endpoint feature requires Spring 5.2 or later.
2. Dependency
For the demo we only add the Spring MVC starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>3. RouterFunction
In functional endpoints the traditional @RequestMapping is replaced by a RouterFunction. The equivalent code is:
@Bean
public RouterFunction<ServerResponse> fooFunction() {
return RouterFunctions.route()
.GET("/v1/userinfo/foo", request -> ServerResponse.ok()
.body("felord.cn"))
.build();
}4. ServerRequest / ServerResponse
ServerRequestabstracts the incoming HTTP request, while ServerResponse abstracts the outgoing response. Both are processed by a HandlerFunction that maps request to response.
5. HandlerFunction
HandlerFunctionis a functional interface that receives a ServerRequest and returns a ServerResponse. Example:
HandlerFunction<ServerResponse> handlerFunction = request ->
ServerResponse.ok().body("felord.cn");6. RequestPredicate
RequestPredicateallows routing decisions based on request method, headers, parameters, etc. The following example routes only when the query parameter plan is present:
@Bean
public RouterFunction<ServerResponse> predicateFunction() {
return RouterFunctions.route()
.GET("/v1/userinfo/predicate",
request -> request.param("plan").isPresent(),
request -> ServerResponse.ok().body("felord.cn"))
.build();
}Calling GET http://localhost:8080/v1/userinfo/predicate?plan=... returns felord.cn with status 200, while a request without the plan parameter results in a 404 response.
7. Summary
Functional endpoints are a new programming model introduced in Spring 5 (supported from 5.2) that aligns with functional programming practices. This article covered the key concepts; a follow‑up will explore more advanced usage.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
