Package org.springframework.web.servlet.function

# Class RouterFunctions

java.lang.Object
org.springframework.web.servlet.function.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.

Since:
  5.2
Author:
  Arjen Poutsma, Sebastien Deleuze

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static interface`
`RouterFunctions.Builder`
Represents a discoverable builder for router functions.
`static interface`
`RouterFunctions.Visitor`
Receives notifications from the logical structure of router functions.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`MATCHING_PATTERN_ATTRIBUTE`
Name of the request attribute that contains the matching pattern, as a `PathPattern`.
`static final String`
`REQUEST_ATTRIBUTE`
Name of the request attribute that contains the `ServerRequest`.
`static final String`
`URI_TEMPLATE_VARIABLES_ATTRIBUTE`
Name of the request attribute that contains the URI templates map, mapping variable names to values.

## Constructor Summary

Constructors
Constructor
Description
`RouterFunctions()`

## Method Summary

Modifier and Type
Method
Description
`static <T extends ServerResponse>
RouterFunction<T>`
`changeParser(RouterFunction<T> routerFunction, org.springframework.web.util.pattern.PathPatternParser parser)`
Changes the `PathPatternParser` on the given router function.
`static <T extends ServerResponse>
RouterFunction<T>`
`nest(RequestPredicate predicate, RouterFunction<T> routerFunction)`
Route to the given router function if the given request predicate applies.
`static RouterFunction<ServerResponse>`
`resource(RequestPredicate predicate, org.springframework.core.io.Resource resource)`
Route requests that match the given predicate to the given resource.
`static RouterFunction<ServerResponse>`
`resource(RequestPredicate predicate, org.springframework.core.io.Resource resource, BiConsumer<org.springframework.core.io.Resource,org.springframework.http.HttpHeaders> headersConsumer)`
Route requests that match the given predicate to the given resource.
`static Function<ServerRequest,Optional<org.springframework.core.io.Resource>>`
`resourceLookupFunction(String pattern, org.springframework.core.io.Resource location)`
Returns the resource lookup function used by `resources(String, Resource)`.
`static RouterFunction<ServerResponse>`
`resources(String pattern, org.springframework.core.io.Resource location)`
Route requests that match the given pattern to resources relative to the given root location.
`static RouterFunction<ServerResponse>`
`resources(String pattern, org.springframework.core.io.Resource location, BiConsumer<org.springframework.core.io.Resource,org.springframework.http.HttpHeaders> headersConsumer)`
Route requests that match the given pattern to resources relative to the given root location.
`static RouterFunction<ServerResponse>`
`resources(Function<ServerRequest,Optional<org.springframework.core.io.Resource>> lookupFunction)`
Route to resources using the provided lookup function.
`static RouterFunction<ServerResponse>`
`resources(Function<ServerRequest,Optional<org.springframework.core.io.Resource>> lookupFunction, BiConsumer<org.springframework.core.io.Resource,org.springframework.http.HttpHeaders> headersConsumer)`
Route to resources using the provided lookup function.
`static RouterFunctions.Builder`
`route()`
Offers a discoverable way to create router functions through a builder-style interface.
`static <T extends ServerResponse>
RouterFunction<T>`
`route(RequestPredicate predicate, HandlerFunction<T> handlerFunction)`
Route to the given handler function if the given request predicate applies.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### REQUEST_ATTRIBUTE

public static final String REQUEST_ATTRIBUTE
Name of the request attribute that contains the `ServerRequest`.

### URI_TEMPLATE_VARIABLES_ATTRIBUTE

public static final String URI_TEMPLATE_VARIABLES_ATTRIBUTE
Name of the request attribute that contains the URI templates map, mapping variable names to values.

### MATCHING_PATTERN_ATTRIBUTE

public static final String MATCHING_PATTERN_ATTRIBUTE
Name of the request attribute that contains the matching pattern, as a `PathPattern`.

## Constructor Details

### RouterFunctions

public RouterFunctions()

## Method Details

### route

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

Returns:
  a router function builder

### route

public static <T extends ServerResponse> RouterFunction<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);

Type Parameters:
  `T` - the type of response returned by the handler function
Parameters:
  `predicate` - the predicate to test
  `handlerFunction` - the handler function to route to if the predicate applies
Returns:
  a router function that routes to `handlerFunction` if `predicate` evaluates to `true`
See Also:
  - `RequestPredicates`

### nest

public static <T extends ServerResponse> RouterFunction<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);

Type Parameters:
  `T` - the type of response returned by the handler function
Parameters:
  `predicate` - the predicate to test
  `routerFunction` - the nested router function to delegate to if the predicate applies
Returns:
  a router function that routes to `routerFunction` if `predicate` evaluates to `true`
See Also:
  - `RequestPredicates`

### resource

public static RouterFunction<ServerResponse> resource(RequestPredicate predicate, org.springframework.core.io.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` - the predicate to match
  `resource` - the resources to serve
Returns:
  a router function that routes to a resource
Since:
  6.1.4

### resource

public static RouterFunction<ServerResponse> resource(RequestPredicate predicate, org.springframework.core.io.Resource resource, BiConsumer<org.springframework.core.io.Resource,org.springframework.http.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 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

public static RouterFunction<ServerResponse> resources(String pattern, org.springframework.core.io.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:
  a router function that routes to resources
See Also:
  - `PathPattern`
  - `resourceLookupFunction(String, Resource)`

### resources

public static RouterFunction<ServerResponse> resources(String pattern, org.springframework.core.io.Resource location, BiConsumer<org.springframework.core.io.Resource,org.springframework.http.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:
  a router function that routes to resources
Since:
  6.1
See Also:
  - `PathPattern`
  - `resourceLookupFunction(String, Resource)`

### resourceLookupFunction

public static Function<ServerRequest,Optional<org.springframework.core.io.Resource>> resourceLookupFunction(String pattern, org.springframework.core.io.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 FileUrlResource("public-resources/"))
         .andThen(resource -> resource.or(() -> defaultResource));
     RouterFunction<ServerResponse> resources = RouterFunctions.resources(lookupFunction);

Parameters:
  `pattern` - the pattern to match
  `location` - the location directory relative to which resources should be resolved
Returns:
  the default resource lookup function for the given parameters.
See Also:
  - `PathPattern`

### resources

public static RouterFunction<ServerResponse> resources(Function<ServerRequest,Optional<org.springframework.core.io.Resource>> lookupFunction)
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:
  a router function that routes to resources

### resources

public static RouterFunction<ServerResponse> resources(Function<ServerRequest,Optional<org.springframework.core.io.Resource>> lookupFunction, BiConsumer<org.springframework.core.io.Resource,org.springframework.http.HttpHeaders> headersConsumer)
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:
  a router function that routes to resources
Since:
  6.1

### changeParser

public static <T extends ServerResponse> RouterFunction<T> changeParser(RouterFunction<T> routerFunction, org.springframework.web.util.pattern.PathPatternParser parser)
Changes the `PathPatternParser` on the given router function. This method can be used to change the `PathPatternParser` properties from the defaults, for instance to change case sensitivity.

Type Parameters:
  `T` - the type of response returned by the handler function
Parameters:
  `routerFunction` - the router function to change the parser in
  `parser` - the parser to change to
Returns:
  the change router function