@Override public RouterFunctions.Builder nest(RequestPredicate predicate, Supplier<RouterFunction<ServerResponse>> routerFunctionSupplier) { Assert.notNull(routerFunctionSupplier, "RouterFunction Supplier must not be null"); RouterFunction<ServerResponse> nestedRoute = routerFunctionSupplier.get(); this.routerFunctions.add(RouterFunctions.nest(predicate, nestedRoute)); return this; }
/** * Return a composed routing function that routes to the given router function if this * route does not match and the given request predicate applies. This method is a convenient * combination of {@link #and(RouterFunction)} and * {@link RouterFunctions#nest(RequestPredicate, RouterFunction)}. * @param predicate the predicate to test if this route does not match * @param routerFunction the router function to route to if this route does not match and * the predicate applies * @return a composed function that route to {@code routerFunction} if this route does not * match and if {@code predicate} applies */ default RouterFunction<T> andNest(RequestPredicate predicate, RouterFunction<T> routerFunction) { return and(RouterFunctions.nest(predicate, routerFunction)); }
@Override public RouterFunctions.Builder nest(RequestPredicate predicate, Consumer<RouterFunctions.Builder> builderConsumer) { Assert.notNull(builderConsumer, "Consumer must not be null"); RouterFunctionBuilder nestedBuilder = new RouterFunctionBuilder(); builderConsumer.accept(nestedBuilder); RouterFunction<ServerResponse> nestedRoute = nestedBuilder.build(); this.routerFunctions.add(RouterFunctions.nest(predicate, nestedRoute)); return this; }
@Bean public RouterFunction<ServerResponse> attributesRouterFunction(AttributesHandler attributesHandler) { return nest(RequestPredicates.GET("/attributes"), route(RequestPredicates.GET("/{foo}"), attributesHandler::attributes)); } }
@Test public void nestNoMatch() { HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build(); RouterFunction<ServerResponse> routerFunction = request -> Mono.just(handlerFunction); MockServerRequest request = MockServerRequest.builder().build(); RequestPredicate requestPredicate = mock(RequestPredicate.class); when(requestPredicate.nest(request)).thenReturn(Optional.empty()); RouterFunction<ServerResponse> result = RouterFunctions.nest(requestPredicate, routerFunction); assertNotNull(result); Mono<HandlerFunction<ServerResponse>> resultHandlerFunction = result.route(request); StepVerifier.create(resultHandlerFunction) .expectComplete() .verify(); }
@Test public void nestMatch() { HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build(); RouterFunction<ServerResponse> routerFunction = request -> Mono.just(handlerFunction); MockServerRequest request = MockServerRequest.builder().build(); RequestPredicate requestPredicate = mock(RequestPredicate.class); when(requestPredicate.nest(request)).thenReturn(Optional.of(request)); RouterFunction<ServerResponse> result = RouterFunctions.nest(requestPredicate, routerFunction); assertNotNull(result); Mono<HandlerFunction<ServerResponse>> resultHandlerFunction = result.route(request); StepVerifier.create(resultHandlerFunction) .expectNext(handlerFunction) .expectComplete() .verify(); }
@Override protected RouterFunction<?> routerFunction() { NestedHandler nestedHandler = new NestedHandler(); return nest(path("/foo/"), route(GET("/bar"), nestedHandler::pattern) .andRoute(GET("/baz"), nestedHandler::pattern)) .andNest(GET("/{foo}"), route(GET("/bar"), nestedHandler::variables).and( nest(GET("/{bar}"), route(GET("/{baz}"), nestedHandler::variables)))) .andRoute(path("/{qux}/quux").and(method(HttpMethod.GET)), nestedHandler::variables) .andRoute(all(), nestedHandler::variables); }
public RouterFunction<ServerResponse> routes() { return nest(path("/test"), route(POST("/helloWorld2").and(accept(APPLICATION_JSON)), this::postHandler) .and(route(POST("/helloWorld"), this::getHandler) .and(route(GET("/rewrite"), this::rewriteHandler)) .and(route(GET("/pdm"), this::pdmHttpGet)) .and(route(GET("/oms"), this::omsHttpGet)) .and(route(GET("/timeout"), this::testRetry)))); }
static RouterFunction<?> router() { return nest(accept(APPLICATION_JSON), route(GET("/test"), req -> Mono .error(new IllegalArgumentException()) .flatMap(a -> ServerResponse.ok().build()))); } }
@Bean public RouterFunction<ServerResponse> routerFunctions() { return nest(path("/api/users"), nest(accept(APPLICATION_JSON), route(GET("/{id}"), userHandler::getUserById) .andRoute(method(HttpMethod.GET), userHandler::getAllUsers) .andRoute(DELETE("/{id}"), userHandler::deleteUser) .andRoute(POST("/"), userHandler::saveUser))); }
static RouterFunction<?> doRoute(final ApiHandler apiHandler, final ErrorHandler errorHandler) { return nest(path(API_PATH), nest(accept(APPLICATION_JSON), route(GET(LOCATION_WITH_ADDRESS_PATH), apiHandler::getLocation) .andRoute(POST(LOCATION_PATH), apiHandler::postLocation) ).andOther(route(RequestPredicates.all(), errorHandler::notFound)) ); } }
@Bean public RouterFunction<ServerResponse> routerFunction() { return nest(path("/test/{something}"), route(GET("/info"), this::variables) .andRoute(GET("/info/{foo}"), this::variables)) .andRoute(method(HttpMethod.GET), this::variables); }
public RouterFunction<ServerResponse> routingFunction() { PersonRepository repository = new DummyPersonRepository(); PersonHandler handler = new PersonHandler(repository); return nest(path("/person"), nest(accept(APPLICATION_JSON), route(GET("/{id}"), handler::getPerson) .andRoute(method(HttpMethod.GET), handler::listPeople) ).andRoute(POST("/").and(contentType(APPLICATION_JSON)), handler::createPerson)); }
@Bean RouterFunction routes(final HandlerFunction<ServerResponse> fallbackHandler) { return nest( path("/"), nest( accept(APPLICATION_JSON), route( GET("/api/orders"), getOrdersHandler() ) ).andNest( accept(APPLICATION_JSON), route( GET("/api"), getCountQueryHandler() ) ) ).andOther( route( GET("/**"), fallbackHandler ) ) ; } }