public static interface RouterFunctions.Builder
RouterFunctions.route()
.Modifier and Type | Method and Description |
---|---|
RouterFunctions.Builder |
add(RouterFunction<ServerResponse> routerFunction)
Adds the given route to this builder.
|
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.
|
RouterFunctions.Builder |
before(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 the
RouterFunction . |
RouterFunctions.Builder |
DELETE(HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles HTTP
DELETE requests. |
RouterFunctions.Builder |
DELETE(RequestPredicate predicate,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
DELETE requests
that match the given predicate. |
RouterFunctions.Builder |
DELETE(String pattern,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
DELETE requests
that match the given pattern. |
RouterFunctions.Builder |
DELETE(String pattern,
RequestPredicate predicate,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
DELETE requests
that match the given pattern and predicate. |
RouterFunctions.Builder |
filter(HandlerFilterFunction<ServerResponse,ServerResponse> filterFunction)
Filters all routes created by this builder with the given filter function.
|
RouterFunctions.Builder |
GET(HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles HTTP
GET requests. |
RouterFunctions.Builder |
GET(RequestPredicate predicate,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
GET requests
that match the given predicate. |
RouterFunctions.Builder |
GET(String pattern,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
GET requests
that match the given pattern. |
RouterFunctions.Builder |
GET(String pattern,
RequestPredicate predicate,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
GET requests
that match the given pattern and predicate. |
RouterFunctions.Builder |
HEAD(HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles HTTP
HEAD requests. |
RouterFunctions.Builder |
HEAD(RequestPredicate predicate,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
HEAD requests
that match the given predicate. |
RouterFunctions.Builder |
HEAD(String pattern,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
HEAD requests
that match the given pattern. |
RouterFunctions.Builder |
HEAD(String pattern,
RequestPredicate predicate,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
HEAD requests
that match the given pattern and predicate. |
RouterFunctions.Builder |
nest(RequestPredicate predicate,
Consumer<RouterFunctions.Builder> builderConsumer)
Route to a built router function if the given request predicate applies.
|
RouterFunctions.Builder |
nest(RequestPredicate predicate,
Supplier<RouterFunction<ServerResponse>> routerFunctionSupplier)
Route to the supplied router function if the given request predicate applies.
|
RouterFunctions.Builder |
onError(Class<? extends Throwable> exceptionType,
BiFunction<Throwable,ServerRequest,ServerResponse> responseProvider)
Filters all exceptions of the given type by applying the given response provider
function.
|
RouterFunctions.Builder |
onError(Predicate<Throwable> predicate,
BiFunction<Throwable,ServerRequest,ServerResponse> responseProvider)
Filters all exceptions that match the predicate by applying the given response provider
function.
|
RouterFunctions.Builder |
OPTIONS(HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles HTTP
OPTIONS requests. |
RouterFunctions.Builder |
OPTIONS(RequestPredicate predicate,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
OPTIONS requests
that match the given predicate. |
RouterFunctions.Builder |
OPTIONS(String pattern,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
OPTIONS requests
that match the given pattern. |
RouterFunctions.Builder |
OPTIONS(String pattern,
RequestPredicate predicate,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
OPTIONS requests
that match the given pattern and predicate. |
RouterFunctions.Builder |
PATCH(HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles HTTP
PATCH requests. |
RouterFunctions.Builder |
PATCH(RequestPredicate predicate,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
PATCH requests
that match the given predicate. |
RouterFunctions.Builder |
PATCH(String pattern,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
PATCH requests
that match the given pattern. |
RouterFunctions.Builder |
PATCH(String pattern,
RequestPredicate predicate,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
PATCH requests
that match the given pattern and predicate. |
RouterFunctions.Builder |
path(String pattern,
Consumer<RouterFunctions.Builder> builderConsumer)
Route to a built router function if the given path prefix pattern applies.
|
RouterFunctions.Builder |
path(String pattern,
Supplier<RouterFunction<ServerResponse>> routerFunctionSupplier)
Route to the supplied router function if the given path prefix pattern applies.
|
RouterFunctions.Builder |
POST(HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles HTTP
POST requests. |
RouterFunctions.Builder |
POST(RequestPredicate predicate,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
POST requests
that match the given predicate. |
RouterFunctions.Builder |
POST(String pattern,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
POST requests
that match the given pattern. |
RouterFunctions.Builder |
POST(String pattern,
RequestPredicate predicate,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
POST requests
that match the given pattern and predicate. |
RouterFunctions.Builder |
PUT(HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles HTTP
PUT requests. |
RouterFunctions.Builder |
PUT(RequestPredicate predicate,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
PUT requests
that match the given predicate. |
RouterFunctions.Builder |
PUT(String pattern,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
PUT requests
that match the given pattern. |
RouterFunctions.Builder |
PUT(String pattern,
RequestPredicate predicate,
HandlerFunction<ServerResponse> handlerFunction)
Adds a route to the given handler function that handles all HTTP
PUT requests
that match the given pattern and predicate. |
RouterFunctions.Builder |
resources(Function<ServerRequest,Optional<Resource>> lookupFunction)
Route to resources using the provided lookup function.
|
RouterFunctions.Builder |
resources(String pattern,
Resource location)
Route requests that match the given pattern to resources relative to the given root location.
|
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.
|
RouterFunctions.Builder |
withAttribute(String name,
Object value)
Add an attribute with the given name and value to the last route built with this builder.
|
RouterFunctions.Builder |
withAttributes(Consumer<Map<String,Object>> attributesConsumer)
Manipulate the attributes of the last route built with the given consumer.
|
RouterFunctions.Builder GET(HandlerFunction<ServerResponse> handlerFunction)
GET
requests.handlerFunction
- the handler function to handle all GET
requestsRouterFunctions.Builder GET(String pattern, HandlerFunction<ServerResponse> handlerFunction)
GET
requests
that match the given pattern.pattern
- the pattern to match tohandlerFunction
- the handler function to handle all GET
requests that
match pattern
RouterFunctions.Builder GET(RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
GET
requests
that match the given predicate.predicate
- predicate to matchhandlerFunction
- the handler function to handle all GET
requests that
match predicate
RequestPredicates
RouterFunctions.Builder GET(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
GET
requests
that match the given pattern and predicate.
For instance, the following example routes GET requests for "/user" that accept JSON
to the listUsers
method in userController
:
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 all GET
requests that
match pattern
and the predicateRequestPredicates
RouterFunctions.Builder HEAD(HandlerFunction<ServerResponse> handlerFunction)
HEAD
requests.handlerFunction
- the handler function to handle all HEAD
requestsRouterFunctions.Builder HEAD(String pattern, HandlerFunction<ServerResponse> handlerFunction)
HEAD
requests
that match the given pattern.pattern
- the pattern to match tohandlerFunction
- the handler function to handle all HEAD
requests that
match pattern
RouterFunctions.Builder HEAD(RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
HEAD
requests
that match the given predicate.predicate
- predicate to matchhandlerFunction
- the handler function to handle all HEAD
requests that
match predicate
RequestPredicates
RouterFunctions.Builder HEAD(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
HEAD
requests
that match the given pattern and predicate.pattern
- the pattern to match topredicate
- additional predicate to matchhandlerFunction
- the handler function to handle all HEAD
requests that
match pattern
RouterFunctions.Builder POST(HandlerFunction<ServerResponse> handlerFunction)
POST
requests.handlerFunction
- the handler function to handle all POST
requestsRouterFunctions.Builder POST(String pattern, HandlerFunction<ServerResponse> handlerFunction)
POST
requests
that match the given pattern.pattern
- the pattern to match tohandlerFunction
- the handler function to handle all POST
requests that
match pattern
RouterFunctions.Builder POST(RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
POST
requests
that match the given predicate.predicate
- predicate to matchhandlerFunction
- the handler function to handle all POST
requests that
match predicate
RequestPredicates
RouterFunctions.Builder POST(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
POST
requests
that match the given pattern and predicate.
For instance, the following example routes POST requests for "/user" that contain JSON
to the addUser
method in userController
:
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 all POST
requests that
match pattern
RouterFunctions.Builder PUT(HandlerFunction<ServerResponse> handlerFunction)
PUT
requests.handlerFunction
- the handler function to handle all PUT
requestsRouterFunctions.Builder PUT(String pattern, HandlerFunction<ServerResponse> handlerFunction)
PUT
requests
that match the given pattern.pattern
- the pattern to match tohandlerFunction
- the handler function to handle all PUT
requests that
match pattern
RouterFunctions.Builder PUT(RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
PUT
requests
that match the given predicate.predicate
- predicate to matchhandlerFunction
- the handler function to handle all PUT
requests that
match predicate
RequestPredicates
RouterFunctions.Builder PUT(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
PUT
requests
that match the given pattern and predicate.
For instance, the following example routes PUT requests for "/user" that contain JSON
to the editUser
method in userController
:
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 all PUT
requests that
match pattern
RouterFunctions.Builder PATCH(HandlerFunction<ServerResponse> handlerFunction)
PATCH
requests.handlerFunction
- the handler function to handle all PATCH
requestsRouterFunctions.Builder PATCH(String pattern, HandlerFunction<ServerResponse> handlerFunction)
PATCH
requests
that match the given pattern.pattern
- the pattern to match tohandlerFunction
- the handler function to handle all PATCH
requests that
match pattern
RouterFunctions.Builder PATCH(RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
PATCH
requests
that match the given predicate.predicate
- predicate to matchhandlerFunction
- the handler function to handle all PATCH
requests that
match predicate
RequestPredicates
RouterFunctions.Builder PATCH(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
PATCH
requests
that match the given pattern and predicate.
For instance, the following example routes PATCH requests for "/user" that contain JSON
to the editUser
method in userController
:
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 all PATCH
requests that
match pattern
RouterFunctions.Builder DELETE(HandlerFunction<ServerResponse> handlerFunction)
DELETE
requests.handlerFunction
- the handler function to handle all DELETE
requestsRouterFunctions.Builder DELETE(String pattern, HandlerFunction<ServerResponse> handlerFunction)
DELETE
requests
that match the given pattern.pattern
- the pattern to match tohandlerFunction
- the handler function to handle all DELETE
requests that
match pattern
RouterFunctions.Builder DELETE(RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
DELETE
requests
that match the given predicate.predicate
- predicate to matchhandlerFunction
- the handler function to handle all DELETE
requests that
match predicate
RequestPredicates
RouterFunctions.Builder DELETE(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
DELETE
requests
that match the given pattern and predicate.pattern
- the pattern to match topredicate
- additional predicate to matchhandlerFunction
- the handler function to handle all DELETE
requests that
match pattern
RouterFunctions.Builder OPTIONS(HandlerFunction<ServerResponse> handlerFunction)
OPTIONS
requests.handlerFunction
- the handler function to handle all OPTIONS
requestsRouterFunctions.Builder OPTIONS(String pattern, HandlerFunction<ServerResponse> handlerFunction)
OPTIONS
requests
that match the given pattern.pattern
- the pattern to match tohandlerFunction
- the handler function to handle all OPTIONS
requests that
match pattern
RouterFunctions.Builder OPTIONS(RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
OPTIONS
requests
that match the given predicate.predicate
- predicate to matchhandlerFunction
- the handler function to handle all OPTIONS
requests that
match predicate
RequestPredicates
RouterFunctions.Builder OPTIONS(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
OPTIONS
requests
that match the given pattern and predicate.pattern
- the pattern to match topredicate
- additional predicate to matchhandlerFunction
- the handler function to handle all OPTIONS
requests that
match pattern
RouterFunctions.Builder route(RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
predicate
- the request predicate to matchhandlerFunction
- the handler function to handle all requests that match the predicateRequestPredicates
RouterFunctions.Builder add(RouterFunction<ServerResponse> routerFunction)
RouterFunctions.route(RequestPredicate, HandlerFunction)
to allow for more flexible predicate matching.
For instance, the following example adds the router function returned from
OrderController.routerFunction()
.
to the changeUser
method in userController
:
RouterFunction<ServerResponse> route = RouterFunctions.route() .GET("/users", userController::listUsers) .add(orderController.routerFunction()); .build();
routerFunction
- the router function to be addedRequestPredicates
RouterFunctions.Builder resources(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 resolvedRouterFunctions.Builder resources(Function<ServerRequest,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
RouterFunctions.Builder nest(RequestPredicate predicate, Supplier<RouterFunction<ServerResponse>> routerFunctionSupplier)
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 appliesRequestPredicates
RouterFunctions.Builder nest(RequestPredicate predicate, Consumer<RouterFunctions.Builder> builderConsumer)
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 a Builder
that provides the nested router
functionRequestPredicates
RouterFunctions.Builder path(String pattern, Supplier<RouterFunction<ServerResponse>> routerFunctionSupplier)
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 to orderController
.
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 matchesRouterFunctions.Builder path(String pattern, Consumer<RouterFunctions.Builder> builderConsumer)
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 a Builder
that provides the nested router
functionRouterFunctions.Builder filter(HandlerFilterFunction<ServerResponse,ServerResponse> filterFunction)
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 builderRouterFunctions.Builder before(Function<ServerRequest,ServerRequest> requestProcessor)
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 requestRouterFunctions.Builder after(BiFunction<ServerRequest,ServerResponse,ServerResponse> responseProcessor)
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 responseRouterFunctions.Builder onError(Predicate<Throwable> predicate, BiFunction<Throwable,ServerRequest,ServerResponse> responseProvider)
For instance, the following example creates a filter that returns a 500 response
status when an IllegalStateException
occurs.
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 responseRouterFunctions.Builder onError(Class<? extends Throwable> exceptionType, BiFunction<Throwable,ServerRequest,ServerResponse> responseProvider)
For instance, the following example creates a filter that returns a 500 response
status when an IllegalStateException
occurs.
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 responseRouterFunctions.Builder withAttribute(String name, Object value)
name
- the attribute namevalue
- the attribute valueRouterFunctions.Builder withAttributes(Consumer<Map<String,Object>> attributesConsumer)
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
Map
methods.
attributesConsumer
- a function that consumes the attributes mapRouterFunction<ServerResponse> build()
RouterFunction
. All created routes are
composed with one another, and filters
(if any) are applied to the result.