A RequestPredicate declares which HTTP requests a functional WebFlux API should handle. RequestPredicates provides helpers such as GET(), while RouterFunction connects the predicate to handler code without annotations. section 12.2
A functional request route brings together four roles:
- RequestPredicate selects the kind of request to handle. section 12.2
- RouterFunction maps matching requests to handler code. section 12.2
- ServerRequest represents the incoming HTTP request. section 12.2
- ServerResponse represents the returned HTTP response. section 12.2
Match GET /hello
route(GET("/hello"), request -> ok().body(just("Hello World!"), String.class)) creates a predicate for GET requests to /hello and sends a reactive Hello World! response through the matching handler. section 12.2
package hello;
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;
import static reactor.core.publisher.Mono.just;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
@Configuration
public class RouterFunctionConfig {
@Bean
public RouterFunction<?> helloRouterFunction() {
return route(GET("/hello"),
request -> ok().body(just("Hello World!"), String.class))
;
}
}
View a complete configuration class that declares the route as a Spring bean and uses static helper imports. section 12.2
