private RouterFunctions.Builder add(RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction) { this.routerFunctions.add(RouterFunctions.route(predicate, handlerFunction)); return this; }
/** * 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)); }
@Bean public RouterFunction<EntityResponse<Person>> monoRouterFunction(PersonHandler personHandler) { return route(RequestPredicates.GET("/mono"), personHandler::mono); }
@Override protected RouterFunction<?> routerFunction() { return RouterFunctions.route(RequestPredicates.path("/"), this::render); }
@Bean public RouterFunction<ServerResponse> fluxRouterFunction(PersonHandler personHandler) { return route(RequestPredicates.GET("/flux"), personHandler::flux); }
@Bean public RouterFunction<ServerResponse> attributesRouterFunction(AttributesHandler attributesHandler) { return nest(RequestPredicates.GET("/attributes"), route(RequestPredicates.GET("/{foo}"), attributesHandler::attributes)); } }
@Bean public RouterFunction<?> handler() { return RouterFunctions.route() .GET("/sessionClassName", request -> request.session().flatMap(session -> { String className = session.getClass().getSimpleName(); return ServerResponse.ok().syncBody(className); })) .build(); }
@Override protected RouterFunction<ServerResponse> routerFunction() { MultipartHandler multipartHandler = new MultipartHandler(); return route(POST("/multipartData"), multipartHandler::multipartData) .andRoute(POST("/parts"), multipartHandler::parts); }
@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); }
@Test public void routeNoMatch() { HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build(); MockServerRequest request = MockServerRequest.builder().build(); RequestPredicate requestPredicate = mock(RequestPredicate.class); when(requestPredicate.test(request)).thenReturn(false); RouterFunction<ServerResponse> result = RouterFunctions.route(requestPredicate, handlerFunction); assertNotNull(result); Mono<HandlerFunction<ServerResponse>> resultHandlerFunction = result.route(request); StepVerifier.create(resultHandlerFunction) .expectComplete() .verify(); }
@Before public void setUp() throws Exception { RouterFunction<?> route = route(GET("/test"), request -> ServerResponse.ok().syncBody("It works!")); this.testClient = WebTestClient.bindToRouterFunction(route).build(); }
@Test public void toHttpHandlerHandlerReturnsException() { HandlerFunction<ServerResponse> handlerFunction = request -> Mono.error(new IllegalStateException()); RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction); HttpHandler result = RouterFunctions.toHttpHandler(routerFunction); assertNotNull(result); MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build(); MockServerHttpResponse httpResponse = new MockServerHttpResponse(); result.handle(httpRequest, httpResponse).block(); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, httpResponse.getStatusCode()); }
@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); }
@Override protected RouterFunction<?> routerFunction() { return RouterFunctions.route(RequestPredicates.GET("/"), request -> ServerResponse.ok().syncBody("FOO")) .andRoute(RequestPredicates.all(), request -> ServerResponse.ok().syncBody("BAR")); }
@Test public void toHttpHandlerNormal() { HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.accepted().build(); RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction); HttpHandler result = RouterFunctions.toHttpHandler(routerFunction); assertNotNull(result); MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build(); MockServerHttpResponse httpResponse = new MockServerHttpResponse(); result.handle(httpRequest, httpResponse).block(); assertEquals(HttpStatus.ACCEPTED, httpResponse.getStatusCode()); }
@Test public void toHttpHandlerHandlerResponseStatusException() { HandlerFunction<ServerResponse> handlerFunction = request -> Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found")); RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction); HttpHandler result = RouterFunctions.toHttpHandler(routerFunction); assertNotNull(result); MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build(); MockServerHttpResponse httpResponse = new MockServerHttpResponse(); result.handle(httpRequest, httpResponse).block(); assertEquals(HttpStatus.NOT_FOUND, httpResponse.getStatusCode()); }
@Before public void start() throws Exception { HttpHandler httpHandler = RouterFunctions.toHttpHandler( route(GET("/test"), request -> ServerResponse.ok().syncBody("It works!"))); this.server = new ReactorHttpServer(); this.server.setHandler(httpHandler); this.server.afterPropertiesSet(); this.server.start(); this.client = WebTestClient.bindToServer() .baseUrl("http://localhost:" + this.server.getPort()) .build(); }