public abstract class RouterFunctions
extends java.lang.Object
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.Modifier and Type | Class and Description |
---|---|
private static class |
RouterFunctions.AbstractRouterFunction<T extends ServerResponse> |
static interface |
RouterFunctions.Builder
Represents a discoverable builder for router functions.
|
private static class |
RouterFunctions.DefaultNestedRouterFunction<T extends ServerResponse> |
private static class |
RouterFunctions.DefaultRouterFunction<T extends ServerResponse> |
(package private) static class |
RouterFunctions.DifferentComposedRouterFunction
A composed routing function that first invokes one function, and then invokes
another function (of a different response type) if this route had
no result.
|
(package private) static class |
RouterFunctions.FilteredRouterFunction<T extends ServerResponse,S extends ServerResponse>
Filter the specified handler functions with the given
filter function.
|
private static class |
RouterFunctions.ResourcesRouterFunction |
(package private) static class |
RouterFunctions.SameComposedRouterFunction<T extends ServerResponse>
A composed routing function that first invokes one function, and then invokes the
another function (of the same response type
T ) if this route had
no result. |
static interface |
RouterFunctions.Visitor
Receives notifications from the logical structure of router functions.
|
Modifier and Type | Field and Description |
---|---|
private static Log |
logger |
static java.lang.String |
MATCHING_PATTERN_ATTRIBUTE
Name of the request attribute that contains the matching pattern, as a
PathPattern . |
static java.lang.String |
REQUEST_ATTRIBUTE
Name of the request attribute that contains the
ServerRequest . |
static java.lang.String |
URI_TEMPLATE_VARIABLES_ATTRIBUTE
Name of the request attribute that contains the URI
templates map, mapping variable names to values.
|
Constructor and Description |
---|
RouterFunctions() |
Modifier and Type | Method and Description |
---|---|
(package private) static <T extends ServerResponse> |
cast(HandlerFunction<?> handlerFunction) |
static <T extends ServerResponse> |
nest(RequestPredicate predicate,
RouterFunction<T> routerFunction)
Route to the given router function if the given request predicate applies.
|
static java.util.function.Function<ServerRequest,java.util.Optional<Resource>> |
resourceLookupFunction(java.lang.String pattern,
Resource location)
Returns the resource lookup function used by
resources(String, Resource) . |
static RouterFunction<ServerResponse> |
resources(java.util.function.Function<ServerRequest,java.util.Optional<Resource>> lookupFunction)
Route to resources using the provided lookup function.
|
static RouterFunction<ServerResponse> |
resources(java.lang.String pattern,
Resource location)
Route requests that match the given pattern to resources relative to the given root location.
|
static RouterFunctions.Builder |
route()
Offers a discoverable way to create router functions through a builder-style interface.
|
static <T extends ServerResponse> |
route(RequestPredicate predicate,
HandlerFunction<T> handlerFunction)
Route to the given handler function if the given request predicate applies.
|
private static final Log logger
public static final java.lang.String REQUEST_ATTRIBUTE
ServerRequest
.public static final java.lang.String URI_TEMPLATE_VARIABLES_ATTRIBUTE
public static final java.lang.String MATCHING_PATTERN_ATTRIBUTE
PathPattern
.public static RouterFunctions.Builder route()
public static <T extends ServerResponse> RouterFunction<T> route(RequestPredicate predicate, HandlerFunction<T> handlerFunction)
For instance, the following example routes GET requests for "/user" to the
listUsers
method in userController
:
RouterFunction<ServerResponse> route = RouterFunctions.route(RequestPredicates.GET("/user"), userController::listUsers);
T
- the type of response returned by the handler functionpredicate
- the predicate to testhandlerFunction
- the handler function to route to if the predicate applieshandlerFunction
if
predicate
evaluates to true
RequestPredicates
public static <T extends ServerResponse> RouterFunction<T> nest(RequestPredicate predicate, RouterFunction<T> routerFunction)
For instance, the following example first creates a composed route that resolves to
listUsers
for a GET, and createUser
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);
T
- the type of response returned by the handler functionpredicate
- the predicate to testrouterFunction
- the nested router function to delegate to if the predicate appliesrouterFunction
if
predicate
evaluates to true
RequestPredicates
public static RouterFunction<ServerResponse> resources(java.lang.String pattern, Resource location)
Resource location = new FileSystemResource("public-resources/"); RouterFunction<ServerResponse> resources = RouterFunctions.resources("/resources/**", location);
pattern
- the pattern to matchlocation
- the location directory relative to which resources should be resolvedresourceLookupFunction(String, Resource)
public static java.util.function.Function<ServerRequest,java.util.Optional<Resource>> resourceLookupFunction(java.lang.String pattern, Resource location)
resources(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);
pattern
- the pattern to matchlocation
- the location directory relative to which resources should be resolvedpublic static RouterFunction<ServerResponse> resources(java.util.function.Function<ServerRequest,java.util.Optional<Resource>> lookupFunction)
Resource
for the given request, it will be it will be exposed using a
HandlerFunction
that handles GET, HEAD, and OPTIONS requests.lookupFunction
- the function to provide a Resource
given the ServerRequest
static <T extends ServerResponse> HandlerFunction<T> cast(HandlerFunction<?> handlerFunction)