Class RouterFunctions
RouterFunction
using a discoverable builder-style API, to
create a RouterFunction
given a RequestPredicate
and HandlerFunction
, and to do further
subrouting on an existing routing
function.
Additionally, this class can transform
a RouterFunction
into an HttpHandler
, which can be run in Servlet
environments, Reactor, or Undertow.
- Since:
- 5.0
- Author:
- Arjen Poutsma, Sebastien Deleuze
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Represents a discoverable builder for router functions.static interface
Receives notifications from the logical structure of router functions. -
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Name of theattribute
that contains the matching pattern, as anPathPattern
.static final String
Name of theServerWebExchange
attribute that contains theServerRequest
.static final String
Name of theServerWebExchange
attribute that contains the URI templates map, mapping variable names to values. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends ServerResponse>
RouterFunction<T>changeParser
(RouterFunction<T> routerFunction, PathPatternParser parser) Changes thePathPatternParser
on the given router function.static <T extends ServerResponse>
RouterFunction<T>nest
(RequestPredicate predicate, RouterFunction<T> routerFunction) Route to the given router function if the given request predicate applies.static RouterFunction<ServerResponse>
resource
(RequestPredicate predicate, Resource resource) Route requests that match the given predicate to the given resource.static RouterFunction<ServerResponse>
resource
(RequestPredicate predicate, Resource resource, BiConsumer<Resource, HttpHeaders> headersConsumer) Route requests that match the given predicate to the given resource.static Function<ServerRequest,
reactor.core.publisher.Mono<Resource>> resourceLookupFunction
(String pattern, Resource location) Returns the resource lookup function used byresources(String, Resource)
.static RouterFunction<ServerResponse>
Route requests that match the given pattern to resources relative to the given root location.static RouterFunction<ServerResponse>
resources
(String pattern, Resource location, BiConsumer<Resource, HttpHeaders> headersConsumer) Route requests that match the given pattern to resources relative to the given root location.static RouterFunction<ServerResponse>
resources
(Function<ServerRequest, reactor.core.publisher.Mono<Resource>> lookupFunction) Route to resources using the provided lookup function.static RouterFunction<ServerResponse>
resources
(Function<ServerRequest, reactor.core.publisher.Mono<Resource>> lookupFunction, BiConsumer<Resource, HttpHeaders> headersConsumer) Route to resources using the provided lookup function.static RouterFunctions.Builder
route()
Offers a discoverable way to create router functions through a builder-style interface.static <T extends ServerResponse>
RouterFunction<T>route
(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Route to the given handler function if the given request predicate applies.static HttpHandler
toHttpHandler
(RouterFunction<?> routerFunction) static HttpHandler
toHttpHandler
(RouterFunction<?> routerFunction, HandlerStrategies strategies) Convert the given router function into anHttpHandler
, using the given strategies.static WebHandler
toWebHandler
(RouterFunction<?> routerFunction) Convert the given router function into aWebHandler
.static WebHandler
toWebHandler
(RouterFunction<?> routerFunction, HandlerStrategies strategies) Convert the given router function into aWebHandler
, using the given strategies.
-
Field Details
-
REQUEST_ATTRIBUTE
Name of theServerWebExchange
attribute that contains theServerRequest
. -
URI_TEMPLATE_VARIABLES_ATTRIBUTE
Name of theServerWebExchange
attribute that contains the URI templates map, mapping variable names to values. -
MATCHING_PATTERN_ATTRIBUTE
Name of theattribute
that contains the matching pattern, as anPathPattern
.
-
-
Constructor Details
-
RouterFunctions
public RouterFunctions()
-
-
Method Details
-
route
Offers a discoverable way to create router functions through a builder-style interface.- Returns:
- a router function builder
- Since:
- 5.1
-
route
public static <T extends ServerResponse> RouterFunction<T> route(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Route to the given handler function if the given request predicate applies.For instance, the following example routes GET requests for "/user" to the
listUsers
method inuserController
:RouterFunction<ServerResponse> route = RouterFunctions.route(RequestPredicates.GET("/user"), userController::listUsers);
- Type Parameters:
T
- the type of response returned by the handler function- Parameters:
predicate
- the predicate to testhandlerFunction
- the handler function to route to if the predicate applies- Returns:
- a router function that routes to
handlerFunction
ifpredicate
evaluates totrue
- See Also:
-
nest
public static <T extends ServerResponse> RouterFunction<T> nest(RequestPredicate predicate, RouterFunction<T> routerFunction) Route to the given router function if the given request predicate applies. This method can be used to create nested routes, where a group of routes share a common path (prefix), header, or other request predicate.For instance, the following example first creates a composed route that resolves to
listUsers
for a GET, andcreateUser
for a POST. This composed route then gets nested with a "/user" path predicate, so that GET requests for "/user" will list users, and POST request for "/user" will create a new user.RouterFunction<ServerResponse> userRoutes = RouterFunctions.route(RequestPredicates.method(HttpMethod.GET), this::listUsers) .andRoute(RequestPredicates.method(HttpMethod.POST), this::createUser); RouterFunction<ServerResponse> nestedRoute = RouterFunctions.nest(RequestPredicates.path("/user"), userRoutes);
- Type Parameters:
T
- the type of response returned by the handler function- Parameters:
predicate
- the predicate to testrouterFunction
- the nested router function to delegate to if the predicate applies- Returns:
- a router function that routes to
routerFunction
ifpredicate
evaluates totrue
- See Also:
-
resource
public static RouterFunction<ServerResponse> resource(RequestPredicate predicate, Resource resource) Route requests that match the given predicate to the given resource. For instanceResource resource = new ClassPathResource("static/index.html") RouterFunction<ServerResponse> resources = RouterFunctions.resource(path("/api/**").negate(), resource);
- Parameters:
predicate
- predicate to matchresource
- the resources to serve- Returns:
- a router function that routes to a resource
- Since:
- 6.1.4
-
resource
public static RouterFunction<ServerResponse> resource(RequestPredicate predicate, Resource resource, BiConsumer<Resource, HttpHeaders> headersConsumer) Route requests that match the given predicate to the given resource. For instanceResource resource = new ClassPathResource("static/index.html") RouterFunction<ServerResponse> resources = RouterFunctions.resource(path("/api/**").negate(), resource);
- Parameters:
predicate
- predicate to matchresource
- the resources to serveheadersConsumer
- provides access to the HTTP headers for served resources- Returns:
- a router function that routes to a resource
- Since:
- 6.1.4
-
resources
Route requests that match the given pattern to resources relative to the given root location. For instanceResource location = new FileSystemResource("public-resources/"); RouterFunction<ServerResponse> resources = RouterFunctions.resources("/resources/**", location);
- Parameters:
pattern
- the pattern to matchlocation
- the location directory relative to which resources should be resolved- Returns:
- a router function that routes to resources
- See Also:
-
resources
public static RouterFunction<ServerResponse> resources(String pattern, Resource location, BiConsumer<Resource, HttpHeaders> headersConsumer) Route requests that match the given pattern to resources relative to the given root location. For instanceResource location = new FileSystemResource("public-resources/"); RouterFunction<ServerResponse> resources = RouterFunctions.resources("/resources/**", location);
- Parameters:
pattern
- the pattern to matchlocation
- the location directory relative to which resources should be resolvedheadersConsumer
- provides access to the HTTP headers for served resources- Returns:
- a router function that routes to resources
- Since:
- 6.1
- See Also:
-
resourceLookupFunction
public static Function<ServerRequest,reactor.core.publisher.Mono<Resource>> resourceLookupFunction(String pattern, Resource location) Returns the resource lookup function used byresources(String, Resource)
. The returned function can be composed on, for instance to return a default resource when the lookup function does not match:Mono<Resource> defaultResource = Mono.just(new ClassPathResource("index.html")); Function<ServerRequest, Mono<Resource>> lookupFunction = RouterFunctions.resourceLookupFunction("/resources/**", new FileSystemResource("public-resources/")) .andThen(resourceMono -> resourceMono.switchIfEmpty(defaultResource)); RouterFunction<ServerResponse> resources = RouterFunctions.resources(lookupFunction);
- Parameters:
pattern
- the pattern to matchlocation
- the location directory relative to which resources should be resolved- Returns:
- the default resource lookup function for the given parameters.
- See Also:
-
resources
public static RouterFunction<ServerResponse> resources(Function<ServerRequest, reactor.core.publisher.Mono<Resource>> lookupFunction) Route to resources using the provided lookup function. If the lookup function provides aResource
for the given request, it will be it will be exposed using aHandlerFunction
that handles GET, HEAD, and OPTIONS requests.- Parameters:
lookupFunction
- the function to provide aResource
given theServerRequest
- Returns:
- a router function that routes to resources
-
resources
public static RouterFunction<ServerResponse> resources(Function<ServerRequest, reactor.core.publisher.Mono<Resource>> lookupFunction, BiConsumer<Resource, HttpHeaders> headersConsumer) Route to resources using the provided lookup function. If the lookup function provides aResource
for the given request, it will be it will be exposed using aHandlerFunction
that handles GET, HEAD, and OPTIONS requests.- Parameters:
lookupFunction
- the function to provide aResource
given theServerRequest
headersConsumer
- provides access to the HTTP headers for served resources- Returns:
- a router function that routes to resources
- Since:
- 6.1
-
toHttpHandler
Convert the given router function into anHttpHandler
, using the default strategies.The returned handler can be adapted to run in the following environments.
- Servlet environments using the
ServletHttpHandlerAdapter
- Reactor using the
ReactorHttpHandlerAdapter
- Undertow using the
UndertowHttpHandlerAdapter
Note that
HttpWebHandlerAdapter
also implementsWebHandler
, allowing for additional filter and exception handler registration throughWebHttpHandlerBuilder
.- Parameters:
routerFunction
- the router function to convert- Returns:
- an HTTP handler that handles HTTP requests using the given router function
- Servlet environments using the
-
toHttpHandler
public static HttpHandler toHttpHandler(RouterFunction<?> routerFunction, HandlerStrategies strategies) Convert the given router function into anHttpHandler
, using the given strategies.The returned handler can be adapted to run in the following environments.
- Servlet environments using the
ServletHttpHandlerAdapter
- Reactor using the
ReactorHttpHandlerAdapter
- Undertow using the
UndertowHttpHandlerAdapter
- Parameters:
routerFunction
- the router function to convertstrategies
- the strategies to use- Returns:
- an HTTP handler that handles HTTP requests using the given router function
- Servlet environments using the
-
toWebHandler
- Parameters:
routerFunction
- the router function to convert- Returns:
- a web handler that handles web request using the given router function
-
toWebHandler
public static WebHandler toWebHandler(RouterFunction<?> routerFunction, HandlerStrategies strategies) Convert the given router function into aWebHandler
, using the given strategies.- Parameters:
routerFunction
- the router function to convertstrategies
- the strategies to use- Returns:
- a web handler that handles web request using the given router function
-
changeParser
public static <T extends ServerResponse> RouterFunction<T> changeParser(RouterFunction<T> routerFunction, PathPatternParser parser) Changes thePathPatternParser
on the given router function. This method can be used to change thePathPatternParser
properties from the defaults, for instance to change case sensitivity.- Type Parameters:
T
- the type of response returned by the handler function- Parameters:
routerFunction
- the router function to change the parser inparser
- the parser to change to.- Returns:
- the change router function
-