类 RouterFunctions
- java.lang.Object
- org.springframework.web.reactive.function.server.RouterFunctions
public abstract class RouterFunctions extends Object
Central entry point to Spring's functional web framework. Exposes routing functionality, such as to create aRouterFunction
using a discoverable builder-style API, to create aRouterFunction
given aRequestPredicate
andHandlerFunction
, and to do further subrouting on an existing routing function.Additionally, this class can transform a
RouterFunction
into anHttpHandler
, which can be run in Servlet 3.1+, Reactor, or Undertow.- 从以下版本开始:
- 5.0
- 作者:
- Arjen Poutsma
嵌套类概要
嵌套类 修饰符和类型 类 说明 static interface
RouterFunctions.Builder
Represents a discoverable builder for router functions.static interface
RouterFunctions.Visitor
Receives notifications from the logical structure of router functions.
字段概要
字段 修饰符和类型 字段 说明 static String
MATCHING_PATTERN_ATTRIBUTE
Name of theattribute
that contains the matching pattern, as aPathPattern
.static String
REQUEST_ATTRIBUTE
Name of theServerWebExchange
attribute that contains theServerRequest
.static String
URI_TEMPLATE_VARIABLES_ATTRIBUTE
Name of theServerWebExchange
attribute that contains the URI templates map, mapping variable names to values.
构造器概要
构造器 构造器 说明 RouterFunctions()
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 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 Function<ServerRequest,reactor.core.publisher.Mono<Resource>>
resourceLookupFunction(String pattern, Resource location)
Returns the resource lookup function used byresources(String, Resource)
.static RouterFunction<ServerResponse>
resources(String pattern, Resource location)
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 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)
Convert the given router function into aHttpHandler
.static HttpHandler
toHttpHandler(RouterFunction<?> routerFunction, HandlerStrategies strategies)
Convert the given router function into aHttpHandler
, 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.
字段详细资料
REQUEST_ATTRIBUTE
public static final String REQUEST_ATTRIBUTE
Name of theServerWebExchange
attribute that contains theServerRequest
.
URI_TEMPLATE_VARIABLES_ATTRIBUTE
public static final String URI_TEMPLATE_VARIABLES_ATTRIBUTE
Name of theServerWebExchange
attribute that contains the URI templates map, mapping variable names to values.
MATCHING_PATTERN_ATTRIBUTE
public static final String MATCHING_PATTERN_ATTRIBUTE
Name of theattribute
that contains the matching pattern, as aPathPattern
.
构造器详细资料
RouterFunctions
public RouterFunctions()
方法详细资料
route
public static RouterFunctions.Builder route()
Offers a discoverable way to create router functions through a builder-style interface.- 返回:
- a router function builder
- 从以下版本开始:
- 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);
- 类型参数:
T
- the type of response returned by the handler function- 参数:
predicate
- the predicate to testhandlerFunction
- the handler function to route to if the predicate applies- 返回:
- a router function that routes to
handlerFunction
ifpredicate
evaluates totrue
- 另请参阅:
RequestPredicates
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);
- 类型参数:
T
- the type of response returned by the handler function- 参数:
predicate
- the predicate to testrouterFunction
- the nested router function to delegate to if the predicate applies- 返回:
- a router function that routes to
routerFunction
ifpredicate
evaluates totrue
- 另请参阅:
RequestPredicates
resources
public static RouterFunction<ServerResponse> resources(String pattern, Resource location)
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);
- 参数:
pattern
- the pattern to matchlocation
- the location directory relative to which resources should be resolved- 返回:
- a router function that routes to resources
- 另请参阅:
resourceLookupFunction(String, Resource)
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);
- 参数:
pattern
- the pattern to matchlocation
- the location directory relative to which resources should be resolved- 返回:
- the default resource lookup function for the given parameters.
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.- 参数:
lookupFunction
- the function to provide aResource
given theServerRequest
- 返回:
- a router function that routes to resources
toHttpHandler
public static HttpHandler toHttpHandler(RouterFunction<?> routerFunction)
Convert the given router function into aHttpHandler
. This conversion uses default strategies.The returned handler can be adapted to run in
- Servlet 3.1+ 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
.- 参数:
routerFunction
- the router function to convert- 返回:
- an http handler that handles HTTP request using the given router function
- Servlet 3.1+ using the
toHttpHandler
public static HttpHandler toHttpHandler(RouterFunction<?> routerFunction, HandlerStrategies strategies)
Convert the given router function into aHttpHandler
, using the given strategies.The returned
HttpHandler
can be adapted to run in- Servlet 3.1+ using the
ServletHttpHandlerAdapter
, - Reactor using the
ReactorHttpHandlerAdapter
, - Undertow using the
UndertowHttpHandlerAdapter
.
- 参数:
routerFunction
- the router function to convertstrategies
- the strategies to use- 返回:
- an http handler that handles HTTP request using the given router function
- Servlet 3.1+ using the
toWebHandler
public static WebHandler toWebHandler(RouterFunction<?> routerFunction)
- 参数:
routerFunction
- the router function to convert- 返回:
- 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.- 参数:
routerFunction
- the router function to convertstrategies
- the strategies to use- 返回:
- a web handler that handles web request using the given router function