Example usage for org.springframework.web.reactive.function.server RouterFunction route

List of usage examples for org.springframework.web.reactive.function.server RouterFunction route

Introduction

In this page you can find the example usage for org.springframework.web.reactive.function.server RouterFunction route.

Prototype

Mono<HandlerFunction<T>> route(ServerRequest request);

Source Link

Document

Return the HandlerFunction handler function that matches the given request.

Usage

From source file:org.springframework.web.reactive.function.server.RouterFunctions.java

/**
 * Convert the given {@linkplain RouterFunction router function} into a {@link WebHandler},
 * using the given strategies./*w w  w  .  ja v a2 s  . c o  m*/
 * @param routerFunction the router function to convert
 * @param strategies the strategies to use
 * @return a web handler that handles web request using the given router function
 */
public static WebHandler toWebHandler(RouterFunction<?> routerFunction, HandlerStrategies strategies) {
    Assert.notNull(routerFunction, "RouterFunction must not be null");
    Assert.notNull(strategies, "HandlerStrategies must not be null");

    return exchange -> {
        ServerRequest request = new DefaultServerRequest(exchange, strategies.messageReaders());
        addAttributes(exchange, request);
        return routerFunction.route(request).defaultIfEmpty(notFound())
                .flatMap(handlerFunction -> wrapException(() -> handlerFunction.handle(request)))
                .flatMap(response -> wrapException(
                        () -> response.writeTo(exchange, new HandlerStrategiesResponseContext(strategies))));
    };
}