/** * 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 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); }
/** * Return a composed routing function that routes to the given handler 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#route(RequestPredicate, HandlerFunction)}. * @param predicate the predicate to test if this route does not match * @param handlerFunction the handler function to route to if this route does not match and * the predicate applies * @return a composed function that route to {@code handlerFunction} if this route does not * match and if {@code predicate} applies */ default RouterFunction<T> andRoute(RequestPredicate predicate, HandlerFunction<T> handlerFunction) { return and(RouterFunctions.route(predicate, handlerFunction)); }
@Test public void and() { HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build(); RouterFunction<ServerResponse> routerFunction1 = request -> Mono.empty(); RouterFunction<ServerResponse> routerFunction2 = request -> Mono.just(handlerFunction); RouterFunction<ServerResponse> result = routerFunction1.and(routerFunction2); assertNotNull(result); MockServerRequest request = MockServerRequest.builder().build(); Mono<HandlerFunction<ServerResponse>> resultHandlerFunction = result.route(request); StepVerifier.create(resultHandlerFunction) .expectNext(handlerFunction) .expectComplete() .verify(); }
@Override protected RouterFunction<?> routerFunction() { RenderingResponseHandler handler = new RenderingResponseHandler(); RouterFunction<RenderingResponse> normalRoute = route(GET("/normal"), handler::render); RouterFunction<RenderingResponse> filteredRoute = route(GET("/filter"), handler::render) .filter(ofResponseProcessor( response -> RenderingResponse.from(response) .modelAttribute("qux", "quux") .build())); return normalRoute.and(filteredRoute); }
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)))); }
/** * Return a composed routing function that first invokes this function, * and then routes to the given handler function if the given request predicate applies. This * method is a convenient combination of {@link #and(RouterFunction)} and * {@link RouterFunctions#route(RequestPredicate, HandlerFunction)}. * @param predicate the predicate to test * @param handlerFunction the handler function to route to * @param <S> the handler function type * @return a composed function that first routes with this function and then the function * created from {@code predicate} and {@code handlerFunction} if this * function has no result */ default <S extends ServerResponse> RouterFunction<?> andRoute(RequestPredicate predicate, HandlerFunction<S> handlerFunction) { return and(RouterFunctions.route(predicate, handlerFunction)); }
@Bean public RouterFunction<?> routerFunction() { return route(GET("/api/user").and(accept(MediaType.APPLICATION_JSON)), userHandler::handleGetUsers) .and(route(GET("/api/user/{id}").and(accept(MediaType.APPLICATION_JSON)), userHandler::handleGetUserById)); // .and(route(POST("/users"), userHandler::handleGetUsers)); }