public abstract class RouterFunctions
extends java.lang.Object
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 3.1+,
Reactor, RxNetty, or Undertow.
And it can transform a
RouterFunction
into an HandlerMapping
, which can be run in a
DispatcherHandler
.
Modifier and Type | Field and Description |
---|---|
private static Log |
logger |
private static HandlerFunction<ServerResponse> |
NOT_FOUND_HANDLER |
static java.lang.String |
REQUEST_ATTRIBUTE
Name of the
ServerWebExchange attribute that contains the ServerRequest . |
static java.lang.String |
URI_TEMPLATE_VARIABLES_ATTRIBUTE
Name of the
ServerWebExchange attribute that contains the URI
templates map, mapping variable names to values. |
Constructor and Description |
---|
RouterFunctions() |
Modifier and Type | Method and Description |
---|---|
private static void |
addAttributes(ServerWebExchange exchange,
ServerRequest request) |
(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.
|
private static <T extends ServerResponse> |
notFound() |
static RouterFunction<ServerResponse> |
resources(java.util.function.Function<ServerRequest,<any>> 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 <T extends ServerResponse> |
route(RequestPredicate predicate,
HandlerFunction<T> handlerFunction)
Route to the given handler function if the given request predicate applies.
|
static HandlerMapping |
toHandlerMapping(RouterFunction<?> routerFunction)
Convert the given
RouterFunction into a HandlerMapping . |
static HandlerMapping |
toHandlerMapping(RouterFunction<?> routerFunction,
HandlerStrategies strategies)
Convert the given router function into a
HandlerMapping ,
using the given strategies. |
static HttpWebHandlerAdapter |
toHttpHandler(RouterFunction<?> routerFunction)
Convert the given router function into a
HttpHandler . |
static HttpWebHandlerAdapter |
toHttpHandler(RouterFunction<?> routerFunction,
HandlerStrategies strategies)
Convert the given router function into a
HttpHandler ,
using the given strategies. |
private static final Log logger
public static final java.lang.String REQUEST_ATTRIBUTE
ServerWebExchange
attribute that contains the ServerRequest
.public static final java.lang.String URI_TEMPLATE_VARIABLES_ATTRIBUTE
ServerWebExchange
attribute that contains the URI
templates map, mapping variable names to values.private static final HandlerFunction<ServerResponse> NOT_FOUND_HANDLER
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/"); RoutingFunction<Resource> resources = RouterFunctions.resources("/resources/**", location);
pattern
- the pattern to matchlocation
- the location directory relative to which resources should be resolvedpublic static RouterFunction<ServerResponse> resources(java.util.function.Function<ServerRequest,<any>> 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
public static HttpWebHandlerAdapter toHttpHandler(RouterFunction<?> routerFunction)
HttpHandler
.
This conversion uses default strategies.
The returned handler can be adapted to run in
ServletHttpHandlerAdapter
,ReactorHttpHandlerAdapter
,RxNettyHttpHandlerAdapter
, or UndertowHttpHandlerAdapter
.Note that HttpWebHandlerAdapter
also implements WebHandler
, allowing
for additional filter and exception handler registration through
WebHttpHandlerBuilder
.
routerFunction
- the router function to convertpublic static HttpWebHandlerAdapter toHttpHandler(RouterFunction<?> routerFunction, HandlerStrategies strategies)
HttpHandler
,
using the given strategies.
The returned HttpHandler
can be adapted to run in
ServletHttpHandlerAdapter
,ReactorHttpHandlerAdapter
,RxNettyHttpHandlerAdapter
, or UndertowHttpHandlerAdapter
.Note that HttpWebHandlerAdapter
also implements WebHandler
, allowing
for additional filter and exception handler registration through
routerFunction
- the router function to convertstrategies
- the strategies to usepublic static HandlerMapping toHandlerMapping(RouterFunction<?> routerFunction)
RouterFunction
into a HandlerMapping
.
This conversion uses default strategies.
The returned HandlerMapping
can be run in a
DispatcherHandler
.
routerFunction
- the router function to convertHandlerFunctionAdapter
,
ServerResponseResultHandler
public static HandlerMapping toHandlerMapping(RouterFunction<?> routerFunction, HandlerStrategies strategies)
HandlerMapping
,
using the given strategies.
The returned HandlerMapping
can be run in a
DispatcherHandler
.
routerFunction
- the router function to convertstrategies
- the strategies to useHandlerFunctionAdapter
,
ServerResponseResultHandler
private static void addAttributes(ServerWebExchange exchange, ServerRequest request)
private static <T extends ServerResponse> HandlerFunction<T> notFound()
static <T extends ServerResponse> HandlerFunction<T> cast(HandlerFunction<?> handlerFunction)