From 59f6044c099e88a25989a6d55ba0b4fe38b29bb3 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 7 May 2019 15:06:59 +0200 Subject: [PATCH] Add route(RequestPredicate, HandlerFunction) to RouterFunctions builder Closes gh-22701 --- .../server/RouterFunctionBuilder.java | 8 +++++- .../function/server/RouterFunctions.java | 12 +++++++++ .../server/RouterFunctionBuilderTests.java | 25 +++++++++++++++---- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctionBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctionBuilder.java index d9cf733b28..20e1c0a4be 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctionBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctionBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -140,6 +140,12 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return add(RequestPredicates.OPTIONS(pattern).and(predicate), handlerFunction); } + @Override + public RouterFunctions.Builder route(RequestPredicate predicate, + HandlerFunction handlerFunction) { + return add(RouterFunctions.route(predicate, handlerFunction)); + } + @Override public RouterFunctions.Builder resources(String pattern, Resource location) { return add(RouterFunctions.resources(pattern, location)); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java index 2343d95ca2..a7e79d6a13 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java @@ -477,6 +477,18 @@ public abstract class RouterFunctions { */ Builder OPTIONS(String pattern, RequestPredicate predicate, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles all requests that match the + * given predicate. + * + * @param predicate the request predicate to match + * @param handlerFunction the handler function to handle all requests that match the predicate + * @return this builder + * @since 5.2 + * @see RequestPredicates + */ + Builder route(RequestPredicate predicate, HandlerFunction handlerFunction); + /** * Adds the given route to this builder. Can be used to merge externally defined router * functions into this builder, or can be combined with diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionBuilderTests.java index 17567b32a6..26d71d693c 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionBuilderTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +30,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import static org.junit.Assert.*; +import static org.springframework.web.reactive.function.server.RequestPredicates.HEAD; /** * @author Arjen Poutsma @@ -41,16 +42,16 @@ public class RouterFunctionBuilderTests { RouterFunction route = RouterFunctions.route() .GET("/foo", request -> ServerResponse.ok().build()) .POST("/", RequestPredicates.contentType(MediaType.TEXT_PLAIN), request -> ServerResponse.noContent().build()) + .route(HEAD("/foo"), request -> ServerResponse.accepted().build()) .build(); - System.out.println(route); - MockServerRequest fooRequest = MockServerRequest.builder(). + MockServerRequest getFooRequest = MockServerRequest.builder(). method(HttpMethod.GET). uri(URI.create("http://localhost/foo")) .build(); - Mono responseMono = route.route(fooRequest) - .flatMap(handlerFunction -> handlerFunction.handle(fooRequest)) + Mono responseMono = route.route(getFooRequest) + .flatMap(handlerFunction -> handlerFunction.handle(getFooRequest)) .map(ServerResponse::statusCode) .map(HttpStatus::value); @@ -58,6 +59,20 @@ public class RouterFunctionBuilderTests { .expectNext(200) .verifyComplete(); + MockServerRequest headFooRequest = MockServerRequest.builder(). + method(HttpMethod.HEAD). + uri(URI.create("http://localhost/foo")) + .build(); + + responseMono = route.route(headFooRequest) + .flatMap(handlerFunction -> handlerFunction.handle(getFooRequest)) + .map(ServerResponse::statusCode) + .map(HttpStatus::value); + + StepVerifier.create(responseMono) + .expectNext(202) + .verifyComplete(); + MockServerRequest barRequest = MockServerRequest.builder(). method(HttpMethod.POST). uri(URI.create("http://localhost/"))