类 RouterFunctions


  • public abstract class RouterFunctions
    extends Object
    Central entry point to Spring's functional web framework. Exposes routing functionality, such as to create a RouterFunction using a discoverable builder-style API, to create a RouterFunction given a RequestPredicate and HandlerFunction, and to do further subrouting on an existing routing function.
    从以下版本开始:
    5.2
    作者:
    Arjen Poutsma
    • 方法详细资料

      • route

        public static RouterFunctions.Builder route()
        Offers a discoverable way to create router functions through a builder-style interface.
        返回:
        a router function builder
      • route

        public static <T extends ServerResponseRouterFunction<T> route​(RequestPredicate predicate,
                                                                         HandlerFunction<T> handlerFunction)
        Route to the given handler function if the given request predicate applies.

        For instance, the following example routes GET requests for "/user" to the listUsers method in userController:

         RouterFunction<ServerResponse> route =
             RouterFunctions.route(RequestPredicates.GET("/user"), userController::listUsers);
         
        类型参数:
        T - the type of response returned by the handler function
        参数:
        predicate - the predicate to test
        handlerFunction - the handler function to route to if the predicate applies
        返回:
        a router function that routes to handlerFunction if predicate evaluates to true
        另请参阅:
        RequestPredicates
      • nest

        public static <T extends ServerResponseRouterFunction<T> nest​(RequestPredicate predicate,
                                                                        RouterFunction<T> routerFunction)
        Route to the given 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 first creates a composed route that resolves to listUsers for a GET, and createUser for a POST. This composed route then gets nested 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> userRoutes =
           RouterFunctions.route(RequestPredicates.method(HttpMethod.GET), this::listUsers)
             .andRoute(RequestPredicates.method(HttpMethod.POST), this::createUser);
         RouterFunction<ServerResponse> nestedRoute =
           RouterFunctions.nest(RequestPredicates.path("/user"), userRoutes);
         
        类型参数:
        T - the type of response returned by the handler function
        参数:
        predicate - the predicate to test
        routerFunction - the nested router function to delegate to if the predicate applies
        返回:
        a router function that routes to routerFunction if predicate evaluates to true
        另请参阅:
        RequestPredicates
      • resources

        public static RouterFunction<ServerResponseresources​(String pattern,
                                                               Resource location)
        Route requests that match the given pattern to resources relative to the given root location. For instance
         Resource location = new FileSystemResource("public-resources/");
         RouterFunction<ServerResponse> resources = RouterFunctions.resources("/resources/**", location);
         
        参数:
        pattern - the pattern to match
        location - the location directory relative to which resources should be resolved
        返回:
        a router function that routes to resources
        另请参阅:
        resourceLookupFunction(String, Resource)
      • resourceLookupFunction

        public static Function<ServerRequest,​Optional<Resource>> resourceLookupFunction​(String pattern,
                                                                                              Resource location)
        Returns the resource lookup function used by resources(String, Resource). The returned function can be composed on, for instance to return a default resource when the lookup function does not match:
         Optional<Resource> defaultResource = Optional.of(new ClassPathResource("index.html"));
         Function<ServerRequest, Optional<Resource>> lookupFunction =
           RouterFunctions.resourceLookupFunction("/resources/**", new FileSystemResource("public-resources/"))
             .andThen(resource -> resource.or(() -> defaultResource));
         RouterFunction<ServerResponse> resources = RouterFunctions.resources(lookupFunction);
         
        参数:
        pattern - the pattern to match
        location - the location directory relative to which resources should be resolved
        返回:
        the default resource lookup function for the given parameters.