Router and Handler Function
The functional web module in Spring WebFlux provides an alternative way to define web endpoints using functional programming style, rather than annotation-based controllers. This approach relies on two main components: Router Functions and Handler Functions.
Router Functions:
Router Functions define the routes and mappings for incoming HTTP requests. They determine how incoming requests are matched to handler functions based on the request path, HTTP method, headers, and other criteria. Router Functions are responsible for routing requests to the appropriate handler functions.
Handler Functions:
Handler Functions handle the incoming HTTP requests and produce responses. They contain the business logic for processing requests and generating responses. Handler Functions are invoked when a request matches a route defined by the Router Function, and they perform the actual processing of the request.
Sample Non-Blocking API using Handler and Router Functions:
Let's create a simple non-blocking API using Handler and Router Functions to expose an endpoint that returns a greeting message.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
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 GreetingRouter {
@Bean
public RouterFunction<ServerResponse> greetingRoute(GreetingHandler greetingHandler) {
return route(GET("/hello"), greetingHandler::hello);
}
}
@Configuration
class GreetingHandler {
public Mono<ServerResponse> hello(ServerRequest request) {
return ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue("Hello, World!");
}
}
In this example:
GreetingRouter
is a configuration class defining the Router Function. It creates a route for the/hello
endpoint and delegates the handling to thegreetingHandler
.GreetingHandler
is a handler class containing the logic for handling the/hello
endpoint. It defines thehello
method, which returns aMono<ServerResponse>
containing the greeting message.- The
hello
method returns aMono
representing the response body. In this case, it returns a simple "Hello, World!" message as the response body with a content type oftext/plain
.
This example demonstrates how to define a non-blocking API using Handler and Router Functions in Spring WebFlux. Router Functions are used to define the routes, and Handler Functions handle the incoming requests and produce responses asynchronously. This approach allows for a more functional and declarative style of defining web endpoints in Spring WebFlux applications.
Leave a Comment