Interface RouterFunctions.Builder

Enclosing class:
RouterFunctions

public static interface RouterFunctions.Builder
Represents a discoverable builder for router functions. Obtained via RouterFunctions.route().
  • Method Details

    • GET

      Adds a route to the given handler function that handles HTTP GET requests.
      Parameters:
      handlerFunction - the handler function to handle all GET requests
      Returns:
      this builder
      Since:
      5.3
    • GET

      Adds a route to the given handler function that handles all HTTP GET requests that match the given pattern.
      Parameters:
      pattern - the pattern to match to
      handlerFunction - the handler function to handle all GET requests that match pattern
      Returns:
      this builder
      See Also:
    • GET

      Adds a route to the given handler function that handles all HTTP GET requests that match the given predicate.
      Parameters:
      predicate - predicate to match
      handlerFunction - the handler function to handle all GET requests that match predicate
      Returns:
      this builder
      Since:
      5.3
      See Also:
    • GET

      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.

      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();
       
      Parameters:
      pattern - the pattern to match to
      predicate - additional predicate to match
      handlerFunction - the handler function to handle all GET requests that match pattern and the predicate
      Returns:
      this builder
      See Also:
    • HEAD

      Adds a route to the given handler function that handles HTTP HEAD requests.
      Parameters:
      handlerFunction - the handler function to handle all HEAD requests
      Returns:
      this builder
      Since:
      5.3
    • HEAD

      Adds a route to the given handler function that handles all HTTP HEAD requests that match the given pattern.
      Parameters:
      pattern - the pattern to match to
      handlerFunction - the handler function to handle all HEAD requests that match pattern
      Returns:
      this builder
      See Also:
    • HEAD

      Adds a route to the given handler function that handles all HTTP HEAD requests that match the given predicate.
      Parameters:
      predicate - predicate to match
      handlerFunction - the handler function to handle all HEAD requests that match predicate
      Returns:
      this builder
      Since:
      5.3
      See Also:
    • HEAD

      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.
      Parameters:
      pattern - the pattern to match to
      predicate - additional predicate to match
      handlerFunction - the handler function to handle all HEAD requests that match pattern
      Returns:
      this builder
      See Also:
    • POST

      Adds a route to the given handler function that handles HTTP POST requests.
      Parameters:
      handlerFunction - the handler function to handle all POST requests
      Returns:
      this builder
      Since:
      5.3
    • POST

      Adds a route to the given handler function that handles all HTTP POST requests that match the given pattern.
      Parameters:
      pattern - the pattern to match to
      handlerFunction - the handler function to handle all POST requests that match pattern
      Returns:
      this builder
      See Also:
    • POST

      Adds a route to the given handler function that handles all HTTP POST requests that match the given predicate.
      Parameters:
      predicate - predicate to match
      handlerFunction - the handler function to handle all POST requests that match predicate
      Returns:
      this builder
      Since:
      5.3
      See Also:
    • POST

      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.

      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();
       
      Parameters:
      pattern - the pattern to match to
      predicate - additional predicate to match
      handlerFunction - the handler function to handle all POST requests that match pattern
      Returns:
      this builder
      See Also:
    • PUT

      Adds a route to the given handler function that handles HTTP PUT requests.
      Parameters:
      handlerFunction - the handler function to handle all PUT requests
      Returns:
      this builder
      Since:
      5.3
    • PUT

      Adds a route to the given handler function that handles all HTTP PUT requests that match the given pattern.
      Parameters:
      pattern - the pattern to match to
      handlerFunction - the handler function to handle all PUT requests that match pattern
      Returns:
      this builder
      See Also:
    • PUT

      Adds a route to the given handler function that handles all HTTP PUT requests that match the given predicate.
      Parameters:
      predicate - predicate to match
      handlerFunction - the handler function to handle all PUT requests that match predicate
      Returns:
      this builder
      Since:
      5.3
      See Also:
    • PUT

      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.

      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();
       
      Parameters:
      pattern - the pattern to match to
      predicate - additional predicate to match
      handlerFunction - the handler function to handle all PUT requests that match pattern
      Returns:
      this builder
      See Also:
    • PATCH

      Adds a route to the given handler function that handles HTTP PATCH requests.
      Parameters:
      handlerFunction - the handler function to handle all PATCH requests
      Returns:
      this builder
      Since:
      5.3
    • PATCH

      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.
      Parameters:
      pattern - the pattern to match to
      handlerFunction - the handler function to handle all PATCH requests that match pattern
      Returns:
      this builder
      See Also:
    • PATCH

      Adds a route to the given handler function that handles all HTTP PATCH requests that match the given predicate.
      Parameters:
      predicate - predicate to match
      handlerFunction - the handler function to handle all PATCH requests that match predicate
      Returns:
      this builder
      Since:
      5.3
      See Also:
    • PATCH

      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.

      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();
       
      Parameters:
      pattern - the pattern to match to
      predicate - additional predicate to match
      handlerFunction - the handler function to handle all PATCH requests that match pattern
      Returns:
      this builder
      See Also:
    • DELETE

      Adds a route to the given handler function that handles HTTP DELETE requests.
      Parameters:
      handlerFunction - the handler function to handle all DELETE requests
      Returns:
      this builder
      Since:
      5.3
    • DELETE

      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.
      Parameters:
      pattern - the pattern to match to
      handlerFunction - the handler function to handle all DELETE requests that match pattern
      Returns:
      this builder
      See Also:
    • DELETE

      Adds a route to the given handler function that handles all HTTP DELETE requests that match the given predicate.
      Parameters:
      predicate - predicate to match
      handlerFunction - the handler function to handle all DELETE requests that match predicate
      Returns:
      this builder
      Since:
      5.3
      See Also:
    • DELETE

      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.
      Parameters:
      pattern - the pattern to match to
      predicate - additional predicate to match
      handlerFunction - the handler function to handle all DELETE requests that match pattern
      Returns:
      this builder
      See Also:
    • OPTIONS

      Adds a route to the given handler function that handles HTTP OPTIONS requests.
      Parameters:
      handlerFunction - the handler function to handle all OPTIONS requests
      Returns:
      this builder
      Since:
      5.3
    • OPTIONS

      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.
      Parameters:
      pattern - the pattern to match to
      handlerFunction - the handler function to handle all OPTIONS requests that match pattern
      Returns:
      this builder
      See Also:
    • OPTIONS

      Adds a route to the given handler function that handles all HTTP OPTIONS requests that match the given predicate.
      Parameters:
      predicate - predicate to match
      handlerFunction - the handler function to handle all OPTIONS requests that match predicate
      Returns:
      this builder
      Since:
      5.3
      See Also:
    • OPTIONS

      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.
      Parameters:
      pattern - the pattern to match to
      predicate - additional predicate to match
      handlerFunction - the handler function to handle all OPTIONS requests that match pattern
      Returns:
      this builder
      See Also:
    • route

      Adds a route to the given handler function that handles all requests that match the given predicate.
      Parameters:
      predicate - the request predicate to match
      handlerFunction - the handler function to handle all requests that match the predicate
      Returns:
      this builder
      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 with 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();
       
      Parameters:
      routerFunction - the router function to be added
      Returns:
      this builder
      See Also:
    • resource

      RouterFunctions.Builder resource(RequestPredicate predicate, Resource resource)
      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 - predicate to match
      resource - 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 - predicate to match
      resource - the resources to serve
      headersConsumer - provides access to the HTTP headers for served resources
      Returns:
      a router function that routes to a resource
      Since:
      6.1.4
    • resources

      RouterFunctions.Builder resources(String pattern, Resource location)
      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 match
      location - 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 match
      location - the location directory relative to which resources should be resolved
      headersConsumer - provides access to the HTTP headers for served resources
      Returns:
      this builder
      Since:
      6.1
      See Also:
    • resources

      Route to resources using the provided lookup function. If the lookup function provides a Resource for the given request, it will be it will be exposed using a HandlerFunction that handles GET, HEAD, and OPTIONS requests.
      Parameters:
      lookupFunction - the function to provide a Resource given the ServerRequest
      Returns:
      this builder
    • resources

      Route to resources using the provided lookup function. If the lookup function provides a Resource for the given request, it will be it will be exposed using a HandlerFunction that handles GET, HEAD, and OPTIONS requests.
      Parameters:
      lookupFunction - the function to provide a Resource given the ServerRequest
      headersConsumer - provides access to the HTTP headers for served resources
      Returns:
      this builder
      Since:
      6.1
    • nest

      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();
       
      Parameters:
      predicate - the predicate to test
      routerFunctionSupplier - supplier for the nested router function to delegate to if the predicate applies
      Returns:
      this builder
      See Also:
    • nest

      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();
       
      Parameters:
      predicate - the predicate to test
      builderConsumer - consumer for a Builder that provides the nested router function
      Returns:
      this builder
      See Also:
    • 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 to orderController.

       RouterFunction<ServerResponse> nestedRoute =
         RouterFunctions.route()
           .path("/user", userController::routerFunction)
           .path("/order", orderController::routerFunction)
           .build();
       
      Parameters:
      pattern - the pattern to match to
      routerFunctionSupplier - 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 to
      builderConsumer - consumer for a Builder that provides the nested router function
      Returns:
      this builder
      See Also:
    • filter

      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();
       
      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

      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();
       
      Parameters:
      responseProcessor - a function that transforms the response
      Returns:
      this builder
    • onError

      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 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();
       
      Parameters:
      predicate - the type of exception to filter
      responseProvider - a function that creates a response
      Returns:
      this builder
    • onError

      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.

      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();
       
      Parameters:
      exceptionType - the type of exception to filter
      responseProvider - a function that creates a response
      Returns:
      this builder
    • withAttribute

      RouterFunctions.Builder withAttribute(String name, Object value)
      Add an attribute with the given name and value to the last route built with this builder.
      Parameters:
      name - the attribute name
      value - the attribute value
      Returns:
      this builder
      Since:
      5.3
    • withAttributes

      RouterFunctions.Builder withAttributes(Consumer<Map<String,Object>> attributesConsumer)
      Manipulate the attributes of the last route built with the given consumer.

      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.

      Parameters:
      attributesConsumer - a function that consumes the attributes map
      Returns:
      this builder
      Since:
      5.3
    • build

      Builds the RouterFunction. All created routes are composed with one another, and filters (if any) are applied to the result.
      Returns:
      the built router function