Interface RouterFunctions.Builder
- Enclosing class:
RouterFunctions
RouterFunctions.route().- Since:
- 5.1
- Author:
- Arjen Poutsma, Sebastien Deleuze, Rossen Stoyanchev
-
Method Summary
Modifier and TypeMethodDescription<T extends ServerResponse>
RouterFunctions.Builderadd(RouterFunction<T> routerFunction) Adds the given route to this builder.<T extends ServerResponse, R extends ServerResponse>
RouterFunctions.Builderafter(BiFunction<ServerRequest, T, R> responseProcessor) Filter the response object for all routes created by this builder with the given response processing function.before(Function<ServerRequest, ServerRequest> requestProcessor) Filter the request object for all routes created by this builder with the given request processing function.build()Builds theRouterFunction.<T extends ServerResponse>
RouterFunctions.BuilderDELETE(String pattern, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPDELETErequests that match the given pattern.<T extends ServerResponse>
RouterFunctions.BuilderDELETE(String pattern, RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPDELETErequests that match the given pattern and predicate.<T extends ServerResponse>
RouterFunctions.BuilderDELETE(HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles HTTPDELETErequests.<T extends ServerResponse>
RouterFunctions.BuilderDELETE(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPDELETErequests that match the given predicate.<T extends ServerResponse, R extends ServerResponse>
RouterFunctions.Builderfilter(HandlerFilterFunction<T, R> filterFunction) Filters all routes created by this builder with the given filter function.<T extends ServerResponse>
RouterFunctions.BuilderGET(String pattern, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPGETrequests that match the given pattern.<T extends ServerResponse>
RouterFunctions.BuilderGET(String pattern, RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPGETrequests that match the given pattern and predicate.<T extends ServerResponse>
RouterFunctions.BuilderGET(HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles HTTPGETrequests.<T extends ServerResponse>
RouterFunctions.BuilderGET(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPGETrequests that match the given predicate.<T extends ServerResponse>
RouterFunctions.BuilderHEAD(String pattern, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPHEADrequests that match the given pattern.<T extends ServerResponse>
RouterFunctions.BuilderHEAD(String pattern, RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPHEADrequests that match the given pattern and predicate.<T extends ServerResponse>
RouterFunctions.BuilderHEAD(HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles HTTPHEADrequests.<T extends ServerResponse>
RouterFunctions.BuilderHEAD(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPHEADrequests that match the given predicate.nest(RequestPredicate predicate, Consumer<RouterFunctions.Builder> builderConsumer) Route to a built router function if the given request predicate applies.<T extends ServerResponse>
RouterFunctions.Buildernest(RequestPredicate predicate, Supplier<RouterFunction<T>> 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.<T extends ServerResponse>
RouterFunctions.BuilderonError(Predicate<? super Throwable> predicate, BiFunction<? super Throwable, ServerRequest, reactor.core.publisher.Mono<T>> responseProvider) Filters all exceptions that match the predicate by applying the given response provider function.<T extends ServerResponse>
RouterFunctions.BuilderOPTIONS(String pattern, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPOPTIONSrequests that match the given pattern.<T extends ServerResponse>
RouterFunctions.BuilderOPTIONS(String pattern, RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPOPTIONSrequests that match the given pattern and predicate.<T extends ServerResponse>
RouterFunctions.BuilderOPTIONS(HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles HTTPOPTIONSrequests.<T extends ServerResponse>
RouterFunctions.BuilderOPTIONS(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPOPTIONSrequests that match the given predicate.<T extends ServerResponse>
RouterFunctions.BuilderPATCH(String pattern, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPATCHrequests that match the given pattern.<T extends ServerResponse>
RouterFunctions.BuilderPATCH(String pattern, RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPATCHrequests that match the given pattern and predicate.<T extends ServerResponse>
RouterFunctions.BuilderPATCH(HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles HTTPPATCHrequests.<T extends ServerResponse>
RouterFunctions.BuilderPATCH(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPATCHrequests that match the given predicate.path(String pattern, Consumer<RouterFunctions.Builder> builderConsumer) Route to a built router function if the given path prefix pattern applies.<T extends ServerResponse>
RouterFunctions.Builderpath(String pattern, Supplier<RouterFunction<T>> routerFunctionSupplier) Route to the supplied router function if the given path prefix pattern applies.<T extends ServerResponse>
RouterFunctions.BuilderPOST(String pattern, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPOSTrequests that match the given pattern.<T extends ServerResponse>
RouterFunctions.BuilderPOST(String pattern, RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPOSTrequests that match the given pattern and predicate.<T extends ServerResponse>
RouterFunctions.BuilderPOST(HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles HTTPPOSTrequests.<T extends ServerResponse>
RouterFunctions.BuilderPOST(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPOSTrequests that match the given predicate.<T extends ServerResponse>
RouterFunctions.BuilderPUT(String pattern, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPUTrequests that match the given pattern.<T extends ServerResponse>
RouterFunctions.BuilderPUT(String pattern, RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPUTrequests that match the given pattern and predicate.<T extends ServerResponse>
RouterFunctions.BuilderPUT(HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles HTTPPUTrequests.<T extends ServerResponse>
RouterFunctions.BuilderPUT(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPUTrequests that match the given predicate.resource(RequestPredicate predicate, Resource resource) Route requests that match the given predicate to the given resource.resource(RequestPredicate predicate, Resource resource, BiConsumer<Resource, HttpHeaders> headersConsumer) Route requests that match the given predicate to the given resource.Route requests that match the given pattern to resources relative to the given root location.resources(String pattern, Resource location, BiConsumer<Resource, HttpHeaders> headersConsumer) Route requests that match the given pattern to resources relative to the given root location.resources(Function<ServerRequest, reactor.core.publisher.Mono<Resource>> lookupFunction) Route to resources using the provided lookup function.resources(Function<ServerRequest, reactor.core.publisher.Mono<Resource>> lookupFunction, BiConsumer<Resource, HttpHeaders> headersConsumer) Route to resources using the provided lookup function.<T extends ServerResponse>
RouterFunctions.Builderroute(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all requests that match the given predicate.withAttribute(String name, Object value) Add an attribute to the last route built with this builder.withAttributes(Consumer<Map<String, Object>> attributesConsumer) Manipulate the attributes of the last route built.
-
Method Details
-
GET
Adds a route to the given handler function that handles HTTPGETrequests.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
handlerFunction- handler for GET requests- Returns:
- this builder
- Since:
- 5.3
-
GET
<T extends ServerResponse> RouterFunctions.Builder GET(String pattern, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPGETrequests that match the given pattern.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
pattern- the pattern to match tohandlerFunction- handler for GET requests that matchpattern- Returns:
- this builder
- See Also:
-
GET
<T extends ServerResponse> RouterFunctions.Builder GET(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPGETrequests that match the given predicate.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
predicate- the predicate to matchhandlerFunction- handler for GET requests that matchpredicate- Returns:
- this builder
- Since:
- 5.3
- See Also:
-
GET
<T extends ServerResponse> RouterFunctions.Builder GET(String pattern, RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPGETrequests that match the given pattern and predicate.For instance, the below 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();- Type Parameters:
T- the type of response returned by the handler function- Parameters:
pattern- the pattern to match topredicate- additional predicate to matchhandlerFunction- handler for GET requests that matchpatternand the predicate- Returns:
- this builder
- See Also:
-
HEAD
Adds a route to the given handler function that handles HTTPHEADrequests.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
handlerFunction- handler for HEAD requests- Returns:
- this builder
- Since:
- 5.3
-
HEAD
<T extends ServerResponse> RouterFunctions.Builder HEAD(String pattern, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPHEADrequests that match the given pattern.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
pattern- the pattern to match tohandlerFunction- handler for HEAD requests that matchpattern- Returns:
- this builder
- See Also:
-
HEAD
<T extends ServerResponse> RouterFunctions.Builder HEAD(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPHEADrequests that match the given predicate.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
predicate- the predicate to matchhandlerFunction- handler for HEAD requests that matchpredicate- Returns:
- this builder
- Since:
- 5.3
- See Also:
-
HEAD
<T extends ServerResponse> RouterFunctions.Builder HEAD(String pattern, RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPHEADrequests that match the given pattern and predicate.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
pattern- the pattern to match topredicate- additional predicate to matchhandlerFunction- handler for HEAD requests that matchpattern- Returns:
- this builder
- See Also:
-
POST
Adds a route to the given handler function that handles HTTPPOSTrequests.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
handlerFunction- handler for POST requests- Returns:
- this builder
- Since:
- 5.3
-
POST
<T extends ServerResponse> RouterFunctions.Builder POST(String pattern, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPOSTrequests that match the given pattern.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
pattern- the pattern to match tohandlerFunction- handler for POST requests that matchpattern- Returns:
- this builder
- See Also:
-
POST
<T extends ServerResponse> RouterFunctions.Builder POST(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPOSTrequests that match the given predicate.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
predicate- the predicate to matchhandlerFunction- handler for POST requests that matchpredicate- Returns:
- this builder
- Since:
- 5.3
- See Also:
-
POST
<T extends ServerResponse> RouterFunctions.Builder POST(String pattern, RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPOSTrequests that match the given pattern and predicate.For instance, the below 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();- Type Parameters:
T- the type of response returned by the handler function- Parameters:
pattern- the pattern to match topredicate- additional predicate to matchhandlerFunction- handler for POST requests that matchpattern- Returns:
- this builder
- See Also:
-
PUT
Adds a route to the given handler function that handles HTTPPUTrequests.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
handlerFunction- handler for PUT requests- Returns:
- this builder
- Since:
- 5.3
-
PUT
<T extends ServerResponse> RouterFunctions.Builder PUT(String pattern, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPUTrequests that match the given pattern.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
pattern- the pattern to match tohandlerFunction- handler for PUT requests that matchpattern- Returns:
- this builder
- See Also:
-
PUT
<T extends ServerResponse> RouterFunctions.Builder PUT(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPUTrequests that match the given predicate.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
predicate- the predicate to matchhandlerFunction- handler for PUT requests that matchpredicate- Returns:
- this builder
- Since:
- 5.3
- See Also:
-
PUT
<T extends ServerResponse> RouterFunctions.Builder PUT(String pattern, RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPUTrequests that match the given pattern and predicate.For instance, the below 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();- Type Parameters:
T- the type of response returned by the handler function- Parameters:
pattern- the pattern to match topredicate- additional predicate to matchhandlerFunction- handler for PUT requests that matchpattern- Returns:
- this builder
- See Also:
-
PATCH
Adds a route to the given handler function that handles HTTPPATCHrequests.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
handlerFunction- handler for PATCH requests- Returns:
- this builder
- Since:
- 5.3
-
PATCH
<T extends ServerResponse> RouterFunctions.Builder PATCH(String pattern, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPATCHrequests that match the given pattern.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
pattern- the pattern to match tohandlerFunction- handler for PATCH requests that matchpattern- Returns:
- this builder
- See Also:
-
PATCH
<T extends ServerResponse> RouterFunctions.Builder PATCH(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPATCHrequests that match the given predicate.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
predicate- the predicate to matchhandlerFunction- handler for PATCH requests that matchpredicate- Returns:
- this builder
- Since:
- 5.3
- See Also:
-
PATCH
<T extends ServerResponse> RouterFunctions.Builder PATCH(String pattern, RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPPATCHrequests that match the given pattern and predicate.For instance, the below 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();- Type Parameters:
T- the type of response returned by the handler function- Parameters:
pattern- the pattern to match topredicate- additional predicate to matchhandlerFunction- handler for PATCH requests that matchpattern- Returns:
- this builder
- See Also:
-
DELETE
Adds a route to the given handler function that handles HTTPDELETErequests.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
handlerFunction- handler for DELETE requests- Returns:
- this builder
- Since:
- 5.3
-
DELETE
<T extends ServerResponse> RouterFunctions.Builder DELETE(String pattern, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPDELETErequests that match the given pattern.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
pattern- the pattern to match tohandlerFunction- handler for DELETE requests that matchpattern- Returns:
- this builder
- See Also:
-
DELETE
<T extends ServerResponse> RouterFunctions.Builder DELETE(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPDELETErequests that match the given predicate.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
predicate- the predicate to matchhandlerFunction- handler for DELETE requests that matchpredicate- Returns:
- this builder
- Since:
- 5.3
- See Also:
-
DELETE
<T extends ServerResponse> RouterFunctions.Builder DELETE(String pattern, RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPDELETErequests that match the given pattern and predicate.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
pattern- the pattern to match topredicate- additional predicate to matchhandlerFunction- handler for DELETE requests that matchpattern- Returns:
- this builder
- See Also:
-
OPTIONS
Adds a route to the given handler function that handles HTTPOPTIONSrequests.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
handlerFunction- handler for OPTIONS requests- Returns:
- this builder
- Since:
- 5.3
-
OPTIONS
<T extends ServerResponse> RouterFunctions.Builder OPTIONS(String pattern, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPOPTIONSrequests that match the given pattern.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
pattern- the pattern to match tohandlerFunction- handler for OPTIONS requests that matchpattern- Returns:
- this builder
- See Also:
-
OPTIONS
<T extends ServerResponse> RouterFunctions.Builder OPTIONS(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPOPTIONSrequests that match the given predicate.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
predicate- the predicate to matchhandlerFunction- handler for OPTIONS requests that matchpredicate- Returns:
- this builder
- Since:
- 5.3
- See Also:
-
OPTIONS
<T extends ServerResponse> RouterFunctions.Builder OPTIONS(String pattern, RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all HTTPOPTIONSrequests that match the given pattern and predicate.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
pattern- the pattern to match topredicate- additional predicate to matchhandlerFunction- handler for OPTIONS requests that matchpattern- Returns:
- this builder
- See Also:
-
route
<T extends ServerResponse> RouterFunctions.Builder route(RequestPredicate predicate, HandlerFunction<T> handlerFunction) Adds a route to the given handler function that handles all requests that match the given predicate.- Type Parameters:
T- the type of response returned by the handler function- Parameters:
predicate- the request predicate to matchhandlerFunction- handler for requests that match the predicate- Returns:
- this builder
- Since:
- 5.2
- See Also:
-
add
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 fbelow adds the router function returned from
OrderController.routerFunction()to thechangeUsermethod inuserController:RouterFunction<ServerResponse> route = RouterFunctions.route() .GET("/users", userController::listUsers) .add(orderController.routerFunction()); .build();- Type Parameters:
T- the type of response returned by the handler function- Parameters:
routerFunction- the router function to be added- Returns:
- this builder
- See Also:
-
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- the predicate to matchresource- the resources to serve- Returns:
- a router function that routes to a resource
- Since:
- 6.1.4
-
resource
RouterFunctions.Builder resource(RequestPredicate predicate, Resource resource, BiConsumer<Resource, HttpHeaders> headersConsumer) Route requests that match the given predicate to the given resource. For instance:Resource resource = new ClassPathResource("static/index.html") RouterFunction<ServerResponse> resources = RouterFunctions.resource(path("/api/**").negate(), resource);- Parameters:
predicate- the 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 instance:Resource location = new FileUrlResource("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:
- this builder
- See Also:
-
resources
RouterFunctions.Builder 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 instance:Resource location = new FileUrlResource("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:
- this builder
- Since:
- 6.1
- See Also:
-
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.- Parameters:
lookupFunction- the function to provide aResource- Returns:
- this builder
-
resources
RouterFunctions.Builder 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 aResourcefor the given request, it will be it will be exposed using aHandlerFunctionthat handles GET, HEAD, and OPTIONS requests.- Parameters:
lookupFunction- the function to provide aResourceheadersConsumer- provides access to the HTTP headers for served resources- Returns:
- this builder
- Since:
- 6.1
-
nest
<T extends ServerResponse> RouterFunctions.Builder nest(RequestPredicate predicate, Supplier<RouterFunction<T>> 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 below 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();- Type Parameters:
T- the type of response returned by the handler function- Parameters:
predicate- the predicate to testrouterFunctionSupplier- supplier for the nested router function to delegate to if the predicate applies- Returns:
- this builder
- See Also:
-
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 below 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();- Parameters:
predicate- the predicate to testbuilderConsumer- consumer for aBuilderthat provides the nested router function- Returns:
- this builder
- See Also:
-
path
<T extends ServerResponse> RouterFunctions.Builder path(String pattern, Supplier<RouterFunction<T>> 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 below 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();- Type Parameters:
T- the type of response returned by the handler function- Parameters:
pattern- the pattern to match torouterFunctionSupplier- supplier for the nested router function to delegate to if the pattern matches- Returns:
- this builder
- See Also:
-
path
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();- Parameters:
pattern- the pattern to match tobuilderConsumer- consumer for aBuilderthat provides the nested router function- Returns:
- this builder
- See Also:
-
filter
<T extends ServerResponse, R extends ServerResponse> RouterFunctions.Builder filter(HandlerFilterFunction<T, R> 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();- Type Parameters:
T- the type of the handler function to filterR- the type of the response of the function- Parameters:
filterFunction- the function to filter all routes built by this builder- Returns:
- this builder
-
before
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();- Parameters:
requestProcessor- a function that transforms the request- Returns:
- this builder
-
after
<T extends ServerResponse, R extends ServerResponse> RouterFunctions.Builder after(BiFunction<ServerRequest, T, R> 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();- Type Parameters:
T- the type of the handler function to filterR- the type of the response of the function- Parameters:
responseProcessor- a function that transforms the response- Returns:
- this builder
-
onError
<T extends ServerResponse> RouterFunctions.Builder onError(Predicate<? super Throwable> predicate, BiFunction<? super Throwable, ServerRequest, reactor.core.publisher.Mono<T>> 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();- Type Parameters:
T- the type of response returned by the handler function- Parameters:
predicate- the type of exception to filterresponseProvider- a function that creates a response- Returns:
- 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();- Type Parameters:
T- the type of response returned by the handler function- Parameters:
exceptionType- the type of exception to filterresponseProvider- a function that creates a response- Returns:
- this builder
-
withAttribute
Add an attribute to the last route built with this builder.- Parameters:
name- the attribute namevalue- the attribute value- Returns:
- this builder
- Since:
- 5.3
-
withAttributes
Manipulate the attributes of the last route built.The map provided to the consumer is "live", so that the consumer can be used to overwrite existing attributes, remove attributes, or use any of the other
Mapmethods.- Parameters:
attributesConsumer- a function that consumes the attributes map- Returns:
- this builder
- Since:
- 5.3
-
build
RouterFunction<ServerResponse> build()Builds theRouterFunction. All created routes are composed with one another, and filters (if any) are applied to the result.- Returns:
- the built router function
-