接口 RouterFunctions.Builder
- 封闭类:
- RouterFunctions
public static interface RouterFunctions.Builder
Represents a discoverable builder for router functions. Obtained viaRouterFunctions.route().- 从以下版本开始:
- 5.1
方法概要
所有方法 实例方法 抽象方法 修饰符和类型 方法 说明 RouterFunctions.Builderadd(RouterFunction<ServerResponse> routerFunction)Adds the given route to this builder.RouterFunctions.Builderafter(BiFunction<ServerRequest,ServerResponse,ServerResponse> responseProcessor)Filter the response object for all routes created by this builder with the given response processing function.RouterFunctions.Builderbefore(Function<ServerRequest,ServerRequest> requestProcessor)Filter the request object for all routes created by this builder with the given request processing function.RouterFunction<ServerResponse>build()Builds theRouterFunction.RouterFunctions.BuilderDELETE(String pattern, HandlerFunction<ServerResponse> handlerFunction)Adds a route to the given handler function that handles all HTTPDELETErequests that match the given pattern.RouterFunctions.BuilderDELETE(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)Adds a route to the given handler function that handles all HTTPDELETErequests that match the given pattern and predicate.RouterFunctions.Builderfilter(HandlerFilterFunction<ServerResponse,ServerResponse> filterFunction)Filters all routes created by this builder with the given filter function.RouterFunctions.BuilderGET(String pattern, HandlerFunction<ServerResponse> handlerFunction)Adds a route to the given handler function that handles all HTTPGETrequests that match the given pattern.RouterFunctions.BuilderGET(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)Adds a route to the given handler function that handles all HTTPGETrequests that match the given pattern and predicate.RouterFunctions.BuilderHEAD(String pattern, HandlerFunction<ServerResponse> handlerFunction)Adds a route to the given handler function that handles all HTTPHEADrequests that match the given pattern.RouterFunctions.BuilderHEAD(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)Adds a route to the given handler function that handles all HTTPHEADrequests that match the given pattern and predicate.RouterFunctions.Buildernest(RequestPredicate predicate, Consumer<RouterFunctions.Builder> builderConsumer)Route to a built router function if the given request predicate applies.RouterFunctions.Buildernest(RequestPredicate predicate, Supplier<RouterFunction<ServerResponse>> routerFunctionSupplier)Route to the supplied router function if the given request predicate applies.<T extends Throwable>
RouterFunctions.BuilderonError(Class<T> exceptionType, BiFunction<? super T,ServerRequest,reactor.core.publisher.Mono<ServerResponse>> responseProvider)Filters all exceptions of the given type by applying the given response provider function.RouterFunctions.BuilderonError(Predicate<? super Throwable> predicate, BiFunction<? super Throwable,ServerRequest,reactor.core.publisher.Mono<ServerResponse>> responseProvider)Filters all exceptions that match the predicate by applying the given response provider function.RouterFunctions.BuilderOPTIONS(String pattern, HandlerFunction<ServerResponse> handlerFunction)Adds a route to the given handler function that handles all HTTPOPTIONSrequests that match the given pattern.RouterFunctions.BuilderOPTIONS(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)Adds a route to the given handler function that handles all HTTPOPTIONSrequests that match the given pattern and predicate.RouterFunctions.BuilderPATCH(String pattern, HandlerFunction<ServerResponse> handlerFunction)Adds a route to the given handler function that handles all HTTPPATCHrequests that match the given pattern.RouterFunctions.BuilderPATCH(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)Adds a route to the given handler function that handles all HTTPPATCHrequests that match the given pattern and predicate.RouterFunctions.Builderpath(String pattern, Consumer<RouterFunctions.Builder> builderConsumer)Route to a built router function if the given path prefix pattern applies.RouterFunctions.Builderpath(String pattern, Supplier<RouterFunction<ServerResponse>> routerFunctionSupplier)Route to the supplied router function if the given path prefix pattern applies.RouterFunctions.BuilderPOST(String pattern, HandlerFunction<ServerResponse> handlerFunction)Adds a route to the given handler function that handles all HTTPPOSTrequests that match the given pattern.RouterFunctions.BuilderPOST(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)Adds a route to the given handler function that handles all HTTPPOSTrequests that match the given pattern and predicate.RouterFunctions.BuilderPUT(String pattern, HandlerFunction<ServerResponse> handlerFunction)Adds a route to the given handler function that handles all HTTPPUTrequests that match the given pattern.RouterFunctions.BuilderPUT(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)Adds a route to the given handler function that handles all HTTPPUTrequests that match the given pattern and predicate.RouterFunctions.Builderresources(String pattern, Resource location)Route requests that match the given pattern to resources relative to the given root location.RouterFunctions.Builderresources(Function<ServerRequest,reactor.core.publisher.Mono<Resource>> lookupFunction)Route to resources using the provided lookup function.RouterFunctions.Builderroute(RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)Adds a route to the given handler function that handles all requests that match the given predicate.
方法详细资料
GET
RouterFunctions.Builder GET(String pattern, HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTPGETrequests that match the given pattern.- 参数:
pattern- the pattern to match tohandlerFunction- the handler function to handle allGETrequests that matchpattern- 返回:
- this builder
GET
RouterFunctions.Builder GET(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTPGETrequests that match the given pattern and predicate.For instance, the following example routes GET requests for "/user" that accept JSON to the
listUsersmethod inuserController:RouterFunction<ServerResponse> route = RouterFunctions.route() .GET("/user", RequestPredicates.accept(MediaType.APPLICATION_JSON), userController::listUsers) .build();- 参数:
pattern- the pattern to match topredicate- additional predicate to matchhandlerFunction- the handler function to handle allGETrequests that matchpattern- 返回:
- this builder
- 另请参阅:
RequestPredicates
HEAD
RouterFunctions.Builder HEAD(String pattern, HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTPHEADrequests that match the given pattern.- 参数:
pattern- the pattern to match tohandlerFunction- the handler function to handle allHEADrequests that matchpattern- 返回:
- this builder
HEAD
RouterFunctions.Builder HEAD(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTPHEADrequests that match the given pattern and predicate.- 参数:
pattern- the pattern to match topredicate- additional predicate to matchhandlerFunction- the handler function to handle allHEADrequests that matchpattern- 返回:
- this builder
POST
RouterFunctions.Builder POST(String pattern, HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTPPOSTrequests that match the given pattern.- 参数:
pattern- the pattern to match tohandlerFunction- the handler function to handle allPOSTrequests that matchpattern- 返回:
- this builder
POST
RouterFunctions.Builder POST(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTPPOSTrequests that match the given pattern and predicate.For instance, the following example routes POST requests for "/user" that contain JSON to the
addUsermethod inuserController:RouterFunction<ServerResponse> route = RouterFunctions.route() .POST("/user", RequestPredicates.contentType(MediaType.APPLICATION_JSON), userController::addUser) .build();- 参数:
pattern- the pattern to match topredicate- additional predicate to matchhandlerFunction- the handler function to handle allPOSTrequests that matchpattern- 返回:
- this builder
PUT
RouterFunctions.Builder PUT(String pattern, HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTPPUTrequests that match the given pattern.- 参数:
pattern- the pattern to match tohandlerFunction- the handler function to handle allPUTrequests that matchpattern- 返回:
- this builder
PUT
RouterFunctions.Builder PUT(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTPPUTrequests that match the given pattern and predicate.For instance, the following example routes PUT requests for "/user" that contain JSON to the
editUsermethod inuserController:RouterFunction<ServerResponse> route = RouterFunctions.route() .PUT("/user", RequestPredicates.contentType(MediaType.APPLICATION_JSON), userController::editUser) .build();- 参数:
pattern- the pattern to match topredicate- additional predicate to matchhandlerFunction- the handler function to handle allPUTrequests that matchpattern- 返回:
- this builder
PATCH
RouterFunctions.Builder PATCH(String pattern, HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTPPATCHrequests that match the given pattern.- 参数:
pattern- the pattern to match tohandlerFunction- the handler function to handle allPATCHrequests that matchpattern- 返回:
- this builder
PATCH
RouterFunctions.Builder PATCH(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTPPATCHrequests that match the given pattern and predicate.For instance, the following example routes PATCH requests for "/user" that contain JSON to the
editUsermethod inuserController:RouterFunction<ServerResponse> route = RouterFunctions.route() .PATCH("/user", RequestPredicates.contentType(MediaType.APPLICATION_JSON), userController::editUser) .build();- 参数:
pattern- the pattern to match topredicate- additional predicate to matchhandlerFunction- the handler function to handle allPATCHrequests that matchpattern- 返回:
- this builder
DELETE
RouterFunctions.Builder DELETE(String pattern, HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTPDELETErequests that match the given pattern.- 参数:
pattern- the pattern to match tohandlerFunction- the handler function to handle allDELETErequests that matchpattern- 返回:
- this builder
DELETE
RouterFunctions.Builder DELETE(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTPDELETErequests that match the given pattern and predicate.- 参数:
pattern- the pattern to match topredicate- additional predicate to matchhandlerFunction- the handler function to handle allDELETErequests that matchpattern- 返回:
- this builder
OPTIONS
RouterFunctions.Builder OPTIONS(String pattern, HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTPOPTIONSrequests that match the given pattern.- 参数:
pattern- the pattern to match tohandlerFunction- the handler function to handle allOPTIONSrequests that matchpattern- 返回:
- this builder
OPTIONS
RouterFunctions.Builder OPTIONS(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTPOPTIONSrequests that match the given pattern and predicate.- 参数:
pattern- the pattern to match topredicate- additional predicate to matchhandlerFunction- the handler function to handle allOPTIONSrequests that matchpattern- 返回:
- this builder
route
RouterFunctions.Builder route(RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all requests that match the given predicate.- 参数:
predicate- the request predicate to matchhandlerFunction- the handler function to handle all requests that match the predicate- 返回:
- this builder
- 从以下版本开始:
- 5.2
- 另请参阅:
RequestPredicates
add
RouterFunctions.Builder add(RouterFunction<ServerResponse> routerFunction)
Adds the given route to this builder. Can be used to merge externally defined router functions into this builder, or can be combined withRouterFunctions.route(RequestPredicate, HandlerFunction)to allow for more flexible predicate matching.For instance, the following example adds the router function returned from
OrderController.routerFunction(). to thechangeUsermethod inuserController:RouterFunction<ServerResponse> route = RouterFunctions.route() .GET("/users", userController::listUsers) .add(orderController.routerFunction()); .build();- 参数:
routerFunction- the router function to be added- 返回:
- this builder
- 另请参阅:
RequestPredicates
resources
RouterFunctions.Builder 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- 返回:
- this builder
resources
RouterFunctions.Builder resources(Function<ServerRequest,reactor.core.publisher.Mono<Resource>> lookupFunction)
Route to resources using the provided lookup function. If the lookup function provides aResourcefor the given request, it will be it will be exposed using aHandlerFunctionthat handles GET, HEAD, and OPTIONS requests.- 参数:
lookupFunction- the function to provide aResourcegiven theServerRequest- 返回:
- this builder
nest
RouterFunctions.Builder nest(RequestPredicate predicate, Supplier<RouterFunction<ServerResponse>> routerFunctionSupplier)
Route to the supplied 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 creates a nested route 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> nestedRoute = RouterFunctions.route() .nest(RequestPredicates.path("/user"), () -> RouterFunctions.route() .GET(this::listUsers) .POST(this::createUser) .build()) .build();- 参数:
predicate- the predicate to testrouterFunctionSupplier- supplier for the nested router function to delegate to if the predicate applies- 返回:
- this builder
- 另请参阅:
RequestPredicates
nest
RouterFunctions.Builder nest(RequestPredicate predicate, Consumer<RouterFunctions.Builder> builderConsumer)
Route to a built 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 creates a nested route 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> nestedRoute = RouterFunctions.route() .nest(RequestPredicates.path("/user"), builder -> builder.GET(this::listUsers) .POST(this::createUser)) .build();- 参数:
predicate- the predicate to testbuilderConsumer- consumer for aBuilderthat provides the nested router function- 返回:
- this builder
- 另请参阅:
RequestPredicates
path
RouterFunctions.Builder path(String pattern, Supplier<RouterFunction<ServerResponse>> routerFunctionSupplier)
Route to the supplied router function if the given path prefix pattern applies. This method can be used to create nested routes, where a group of routes share a common path prefix. Specifically, this method can be used to merge externally defined router functions under a path prefix.For instance, the following example creates a nested route with a "/user" path predicate that delegates to the router function defined in
userController, and with a "/order" path that delegates toorderController.RouterFunction<ServerResponse> nestedRoute = RouterFunctions.route() .path("/user", userController::routerFunction) .path("/order", orderController::routerFunction) .build();- 参数:
pattern- the pattern to match torouterFunctionSupplier- supplier for the nested router function to delegate to if the pattern matches- 返回:
- this builder
path
RouterFunctions.Builder path(String pattern, Consumer<RouterFunctions.Builder> builderConsumer)
Route to a built router function if the given path prefix pattern applies. This method can be used to create nested routes, where a group of routes share a common path prefix.For instance, the following example creates a nested route 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> nestedRoute = RouterFunctions.route() .path("/user", builder -> builder.GET(this::listUsers) .POST(this::createUser)) .build();- 参数:
pattern- the pattern to match tobuilderConsumer- consumer for aBuilderthat provides the nested router function- 返回:
- this builder
filter
RouterFunctions.Builder filter(HandlerFilterFunction<ServerResponse,ServerResponse> filterFunction)
Filters all routes created by this builder with the given filter function. Filter functions are typically used to address cross-cutting concerns, such as logging, security, etc.For instance, the following example creates a filter that returns a 401 Unauthorized response if the request does not contain the necessary authentication headers.
RouterFunction<ServerResponse> filteredRoute = RouterFunctions.route() .GET("/user", this::listUsers) .filter((request, next) -> { // check for authentication headers if (isAuthenticated(request)) { return next.handle(request); } else { return ServerResponse.status(HttpStatus.UNAUTHORIZED).build(); } }) .build();- 参数:
filterFunction- the function to filter all routes built by this builder- 返回:
- this builder
before
RouterFunctions.Builder before(Function<ServerRequest,ServerRequest> requestProcessor)
Filter the request object for all routes created by this builder with the given request processing function. Filters are typically used to address cross-cutting concerns, such as logging, security, etc.For instance, the following example creates a filter that logs the request before the handler function executes.
RouterFunction<ServerResponse> filteredRoute = RouterFunctions.route() .GET("/user", this::listUsers) .before(request -> { log(request); return request; }) .build();- 参数:
requestProcessor- a function that transforms the request- 返回:
- this builder
after
RouterFunctions.Builder after(BiFunction<ServerRequest,ServerResponse,ServerResponse> responseProcessor)
Filter the response object for all routes created by this builder with the given response processing function. Filters are typically used to address cross-cutting concerns, such as logging, security, etc.For instance, the following example creates a filter that logs the response after the handler function executes.
RouterFunction<ServerResponse> filteredRoute = RouterFunctions.route() .GET("/user", this::listUsers) .after((request, response) -> { log(response); return response; }) .build();- 参数:
responseProcessor- a function that transforms the response- 返回:
- this builder
onError
RouterFunctions.Builder onError(Predicate<? super Throwable> predicate, BiFunction<? super Throwable,ServerRequest,reactor.core.publisher.Mono<ServerResponse>> responseProvider)
Filters all exceptions that match the predicate by applying the given response provider function.For instance, the following example creates a filter that returns a 500 response status when an
IllegalStateExceptionoccurs.RouterFunction<ServerResponse> filteredRoute = RouterFunctions.route() .GET("/user", this::listUsers) .onError(e -> e instanceof IllegalStateException, (e, request) -> ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).build()) .build();- 参数:
predicate- the type of exception to filterresponseProvider- a function that creates a response- 返回:
- this builder
onError
<T extends Throwable> RouterFunctions.Builder onError(Class<T> exceptionType, BiFunction<? super T,ServerRequest,reactor.core.publisher.Mono<ServerResponse>> responseProvider)
Filters all exceptions of the given type by applying the given response provider function.For instance, the following example creates a filter that returns a 500 response status when an
IllegalStateExceptionoccurs.RouterFunction<ServerResponse> filteredRoute = RouterFunctions.route() .GET("/user", this::listUsers) .onError(IllegalStateException.class, (e, request) -> ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).build()) .build();- 参数:
exceptionType- the type of exception to filterresponseProvider- a function that creates a response- 返回:
- this builder
build
RouterFunction<ServerResponse> build()
Builds theRouterFunction. All created routes are composed with one another, and filters (if any) are applied to the result.- 返回:
- the built router function