class RouterFunctionBuilder extends java.lang.Object implements RouterFunctions.Builder
RouterFunctions.Builder
.Modifier and Type | Field and Description |
---|---|
private java.util.List<HandlerFilterFunction<ServerResponse,ServerResponse>> |
filterFunctions |
private java.util.List<RouterFunction<ServerResponse>> |
routerFunctions |
Constructor and Description |
---|
RouterFunctionBuilder() |
Modifier and Type | Method and Description |
---|---|
private RouterFunctions.Builder |
add(RequestPredicate predicate,
HandlerFunction<ServerResponse> handlerFunction) |
RouterFunctions.Builder |
add(RouterFunction<ServerResponse> routerFunction)
Adds the given route to this builder.
|
RouterFunctions.Builder |
after(java.util.function.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(java.util.function.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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,
java.util.function.Consumer<RouterFunctions.Builder> builderConsumer)
Route to a built router function if the given request predicate applies.
|
RouterFunctions.Builder |
nest(RequestPredicate predicate,
java.util.function.Supplier<RouterFunction<ServerResponse>> routerFunctionSupplier)
Route to the supplied router function if the given request predicate applies.
|
<T extends java.lang.Throwable> |
onError(java.lang.Class<T> exceptionType,
java.util.function.BiFunction<? super T,ServerRequest,<any>> responseProvider)
Filters all exceptions of the given type by applying the given response provider
function.
|
RouterFunctions.Builder |
onError(java.util.function.Predicate<? super java.lang.Throwable> predicate,
java.util.function.BiFunction<? super java.lang.Throwable,ServerRequest,<any>> responseProvider)
Filters all exceptions that match the predicate by applying the given response provider
function.
|
RouterFunctions.Builder |
OPTIONS(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.String pattern,
java.util.function.Consumer<RouterFunctions.Builder> builderConsumer)
Route to a built router function if the given path prefix pattern applies.
|
RouterFunctions.Builder |
path(java.lang.String pattern,
java.util.function.Supplier<RouterFunction<ServerResponse>> routerFunctionSupplier)
Route to the supplied router function if the given path prefix pattern applies.
|
RouterFunctions.Builder |
POST(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.util.function.Function<ServerRequest,<any>> lookupFunction)
Route to resources using the provided lookup function.
|
RouterFunctions.Builder |
resources(java.lang.String pattern,
Resource location)
Route requests that match the given pattern to resources relative to the given root location.
|
private java.util.List<RouterFunction<ServerResponse>> routerFunctions
private java.util.List<HandlerFilterFunction<ServerResponse,ServerResponse>> filterFunctions
public RouterFunctions.Builder add(RouterFunction<ServerResponse> routerFunction)
RouterFunctions.Builder
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
:
RouterFunctionlt;ServerResponsegt; route = RouterFunctions.route() .GET("/users", userController::listUsers) .add(orderController.routerFunction()); .build();
add
in interface RouterFunctions.Builder
routerFunction
- the router function to be addedRequestPredicates
private RouterFunctions.Builder add(RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
public RouterFunctions.Builder GET(java.lang.String pattern, HandlerFunction<ServerResponse> handlerFunction)
RouterFunctions.Builder
GET
requests
that match the given pattern.GET
in interface RouterFunctions.Builder
pattern
- the pattern to match tohandlerFunction
- the handler function to handle all GET
requests that
match pattern
public RouterFunctions.Builder GET(java.lang.String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
RouterFunctions.Builder
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();
GET
in interface RouterFunctions.Builder
pattern
- the pattern to match topredicate
- additional predicate to matchhandlerFunction
- the handler function to handle all GET
requests that
match pattern
RequestPredicates
public RouterFunctions.Builder HEAD(java.lang.String pattern, HandlerFunction<ServerResponse> handlerFunction)
RouterFunctions.Builder
HEAD
requests
that match the given pattern.HEAD
in interface RouterFunctions.Builder
pattern
- the pattern to match tohandlerFunction
- the handler function to handle all HEAD
requests that
match pattern
public RouterFunctions.Builder HEAD(java.lang.String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
RouterFunctions.Builder
HEAD
requests
that match the given pattern and predicate.HEAD
in interface RouterFunctions.Builder
pattern
- the pattern to match topredicate
- additional predicate to matchhandlerFunction
- the handler function to handle all HEAD
requests that
match pattern
public RouterFunctions.Builder POST(java.lang.String pattern, HandlerFunction<ServerResponse> handlerFunction)
RouterFunctions.Builder
POST
requests
that match the given pattern.POST
in interface RouterFunctions.Builder
pattern
- the pattern to match tohandlerFunction
- the handler function to handle all POST
requests that
match pattern
public RouterFunctions.Builder POST(java.lang.String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
RouterFunctions.Builder
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();
POST
in interface RouterFunctions.Builder
pattern
- the pattern to match topredicate
- additional predicate to matchhandlerFunction
- the handler function to handle all POST
requests that
match pattern
public RouterFunctions.Builder PUT(java.lang.String pattern, HandlerFunction<ServerResponse> handlerFunction)
RouterFunctions.Builder
PUT
requests
that match the given pattern.PUT
in interface RouterFunctions.Builder
pattern
- the pattern to match tohandlerFunction
- the handler function to handle all PUT
requests that
match pattern
public RouterFunctions.Builder PUT(java.lang.String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
RouterFunctions.Builder
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();
PUT
in interface RouterFunctions.Builder
pattern
- the pattern to match topredicate
- additional predicate to matchhandlerFunction
- the handler function to handle all PUT
requests that
match pattern
public RouterFunctions.Builder PATCH(java.lang.String pattern, HandlerFunction<ServerResponse> handlerFunction)
RouterFunctions.Builder
PATCH
requests
that match the given pattern.PATCH
in interface RouterFunctions.Builder
pattern
- the pattern to match tohandlerFunction
- the handler function to handle all PATCH
requests that
match pattern
public RouterFunctions.Builder PATCH(java.lang.String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
RouterFunctions.Builder
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();
PATCH
in interface RouterFunctions.Builder
pattern
- the pattern to match topredicate
- additional predicate to matchhandlerFunction
- the handler function to handle all PATCH
requests that
match pattern
public RouterFunctions.Builder DELETE(java.lang.String pattern, HandlerFunction<ServerResponse> handlerFunction)
RouterFunctions.Builder
DELETE
requests
that match the given pattern.DELETE
in interface RouterFunctions.Builder
pattern
- the pattern to match tohandlerFunction
- the handler function to handle all DELETE
requests that
match pattern
public RouterFunctions.Builder DELETE(java.lang.String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
RouterFunctions.Builder
DELETE
requests
that match the given pattern and predicate.DELETE
in interface RouterFunctions.Builder
pattern
- the pattern to match topredicate
- additional predicate to matchhandlerFunction
- the handler function to handle all DELETE
requests that
match pattern
public RouterFunctions.Builder OPTIONS(java.lang.String pattern, HandlerFunction<ServerResponse> handlerFunction)
RouterFunctions.Builder
OPTIONS
requests
that match the given pattern.OPTIONS
in interface RouterFunctions.Builder
pattern
- the pattern to match tohandlerFunction
- the handler function to handle all OPTIONS
requests that
match pattern
public RouterFunctions.Builder OPTIONS(java.lang.String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction)
RouterFunctions.Builder
OPTIONS
requests
that match the given pattern and predicate.OPTIONS
in interface RouterFunctions.Builder
pattern
- the pattern to match topredicate
- additional predicate to matchhandlerFunction
- the handler function to handle all OPTIONS
requests that
match pattern
public RouterFunctions.Builder resources(java.lang.String pattern, Resource location)
RouterFunctions.Builder
Resource location = new FileSystemResource("public-resources/"); RouterFunction<ServerResponse> resources = RouterFunctions.resources("/resources/**", location);
resources
in interface RouterFunctions.Builder
pattern
- the pattern to matchlocation
- the location directory relative to which resources should be resolvedpublic RouterFunctions.Builder resources(java.util.function.Function<ServerRequest,<any>> lookupFunction)
RouterFunctions.Builder
Resource
for the given request, it will be it will be exposed using a
HandlerFunction
that handles GET, HEAD, and OPTIONS requests.resources
in interface RouterFunctions.Builder
lookupFunction
- the function to provide a Resource
given the ServerRequest
public RouterFunctions.Builder nest(RequestPredicate predicate, java.util.function.Consumer<RouterFunctions.Builder> builderConsumer)
RouterFunctions.Builder
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();
nest
in interface RouterFunctions.Builder
predicate
- the predicate to testbuilderConsumer
- consumer for a Builder
that provides the nested router
functionRequestPredicates
public RouterFunctions.Builder nest(RequestPredicate predicate, java.util.function.Supplier<RouterFunction<ServerResponse>> routerFunctionSupplier)
RouterFunctions.Builder
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();
nest
in interface RouterFunctions.Builder
predicate
- the predicate to testrouterFunctionSupplier
- supplier for the nested router function to delegate to if
the predicate appliesRequestPredicates
public RouterFunctions.Builder path(java.lang.String pattern, java.util.function.Consumer<RouterFunctions.Builder> builderConsumer)
RouterFunctions.Builder
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();
path
in interface RouterFunctions.Builder
pattern
- the pattern to match tobuilderConsumer
- consumer for a Builder
that provides the nested router
functionpublic RouterFunctions.Builder path(java.lang.String pattern, java.util.function.Supplier<RouterFunction<ServerResponse>> routerFunctionSupplier)
RouterFunctions.Builder
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();
path
in interface RouterFunctions.Builder
pattern
- the pattern to match torouterFunctionSupplier
- supplier for the nested router function to delegate to if
the pattern matchespublic RouterFunctions.Builder filter(HandlerFilterFunction<ServerResponse,ServerResponse> filterFunction)
RouterFunctions.Builder
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();
filter
in interface RouterFunctions.Builder
filterFunction
- the function to filter all routes built by this builderpublic RouterFunctions.Builder before(java.util.function.Function<ServerRequest,ServerRequest> requestProcessor)
RouterFunctions.Builder
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();
before
in interface RouterFunctions.Builder
requestProcessor
- a function that transforms the requestpublic RouterFunctions.Builder after(java.util.function.BiFunction<ServerRequest,ServerResponse,ServerResponse> responseProcessor)
RouterFunctions.Builder
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();
after
in interface RouterFunctions.Builder
responseProcessor
- a function that transforms the responsepublic RouterFunctions.Builder onError(java.util.function.Predicate<? super java.lang.Throwable> predicate, java.util.function.BiFunction<? super java.lang.Throwable,ServerRequest,<any>> responseProvider)
RouterFunctions.Builder
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();
onError
in interface RouterFunctions.Builder
predicate
- the type of exception to filterresponseProvider
- a function that creates a responsepublic <T extends java.lang.Throwable> RouterFunctions.Builder onError(java.lang.Class<T> exceptionType, java.util.function.BiFunction<? super T,ServerRequest,<any>> responseProvider)
RouterFunctions.Builder
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();
onError
in interface RouterFunctions.Builder
exceptionType
- the type of exception to filterresponseProvider
- a function that creates a responsepublic RouterFunction<ServerResponse> build()
RouterFunctions.Builder
RouterFunction
. All created routes are
composed with one another, and filters
(if any) are applied to the result.build
in interface RouterFunctions.Builder