From 200b33b26a9a0c363d04422747d246870f28d89c Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 6 Oct 2020 11:00:17 +0200 Subject: [PATCH] Add no path HTTP method variants for route builder This commit adds two overloaded methods for each HTTP method in the WebFlux.fn and WebMvc.fn route builders: one method taking just a handler function, the other a request predicate and handler function. After this commit, it is no longer required to provide a String path, which is particularly useful when nesting routes, and the path would be "". Closes gh-25752 --- .../server/RouterFunctionBuilder.java | 95 ++++++++++- .../function/server/RouterFunctions.java | 143 +++++++++++++++- .../function/server/RouterFunctionDsl.kt | 147 +++++++++++++++-- .../function/RouterFunctionBuilder.java | 105 ++++++++++-- .../web/servlet/function/RouterFunctions.java | 153 +++++++++++++++++- .../web/servlet/function/RouterFunctionDsl.kt | 144 +++++++++++++++-- src/docs/asciidoc/web/webflux-functional.adoc | 12 +- src/docs/asciidoc/web/webmvc-functional.adoc | 12 +- 8 files changed, 757 insertions(+), 54 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 07d3c2c5f3..4572ca03ae 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 @@ -28,6 +28,7 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import org.springframework.core.io.Resource; +import org.springframework.http.HttpMethod; import org.springframework.util.Assert; /** @@ -38,9 +39,9 @@ import org.springframework.util.Assert; */ class RouterFunctionBuilder implements RouterFunctions.Builder { - private List> routerFunctions = new ArrayList<>(); + private final List> routerFunctions = new ArrayList<>(); - private List> filterFunctions = new ArrayList<>(); + private final List> filterFunctions = new ArrayList<>(); @Override @@ -50,13 +51,23 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return this; } - private RouterFunctions.Builder add(RequestPredicate predicate, - HandlerFunction handlerFunction) { - + private RouterFunctions.Builder add(RequestPredicate predicate, HandlerFunction handlerFunction) { this.routerFunctions.add(RouterFunctions.route(predicate, handlerFunction)); return this; } + // GET + + @Override + public RouterFunctions.Builder GET(HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.GET), handlerFunction); + } + + @Override + public RouterFunctions.Builder GET(RequestPredicate predicate, HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.GET).and(predicate), handlerFunction); + } + @Override public RouterFunctions.Builder GET(String pattern, HandlerFunction handlerFunction) { return add(RequestPredicates.GET(pattern), handlerFunction); @@ -69,6 +80,18 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return add(RequestPredicates.GET(pattern).and(predicate), handlerFunction); } + // HEAD + + @Override + public RouterFunctions.Builder HEAD(HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.HEAD), handlerFunction); + } + + @Override + public RouterFunctions.Builder HEAD(RequestPredicate predicate, HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.HEAD).and(predicate), handlerFunction); + } + @Override public RouterFunctions.Builder HEAD(String pattern, HandlerFunction handlerFunction) { return add(RequestPredicates.HEAD(pattern), handlerFunction); @@ -81,6 +104,18 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return add(RequestPredicates.HEAD(pattern).and(predicate), handlerFunction); } + // POST + + @Override + public RouterFunctions.Builder POST(HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.POST), handlerFunction); + } + + @Override + public RouterFunctions.Builder POST(RequestPredicate predicate, HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.POST).and(predicate), handlerFunction); + } + @Override public RouterFunctions.Builder POST(String pattern, HandlerFunction handlerFunction) { return add(RequestPredicates.POST(pattern), handlerFunction); @@ -93,6 +128,18 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return add(RequestPredicates.POST(pattern).and(predicate), handlerFunction); } + // PUT + + @Override + public RouterFunctions.Builder PUT(HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.PUT), handlerFunction); + } + + @Override + public RouterFunctions.Builder PUT(RequestPredicate predicate, HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.PUT).and(predicate), handlerFunction); + } + @Override public RouterFunctions.Builder PUT(String pattern, HandlerFunction handlerFunction) { return add(RequestPredicates.PUT(pattern), handlerFunction); @@ -105,6 +152,18 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return add(RequestPredicates.PUT(pattern).and(predicate), handlerFunction); } + // PATCH + + @Override + public RouterFunctions.Builder PATCH(HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.PATCH), handlerFunction); + } + + @Override + public RouterFunctions.Builder PATCH(RequestPredicate predicate, HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.PATCH).and(predicate), handlerFunction); + } + @Override public RouterFunctions.Builder PATCH(String pattern, HandlerFunction handlerFunction) { return add(RequestPredicates.PATCH(pattern), handlerFunction); @@ -117,6 +176,18 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return add(RequestPredicates.PATCH(pattern).and(predicate), handlerFunction); } + // DELETE + + @Override + public RouterFunctions.Builder DELETE(HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.DELETE), handlerFunction); + } + + @Override + public RouterFunctions.Builder DELETE(RequestPredicate predicate, HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.DELETE).and(predicate), handlerFunction); + } + @Override public RouterFunctions.Builder DELETE(String pattern, HandlerFunction handlerFunction) { return add(RequestPredicates.DELETE(pattern), handlerFunction); @@ -129,6 +200,18 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return add(RequestPredicates.DELETE(pattern).and(predicate), handlerFunction); } + // OPTIONS + + @Override + public RouterFunctions.Builder OPTIONS(HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.OPTIONS), handlerFunction); + } + + @Override + public RouterFunctions.Builder OPTIONS(RequestPredicate predicate, HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.OPTIONS).and(predicate), handlerFunction); + } + @Override public RouterFunctions.Builder OPTIONS(String pattern, HandlerFunction handlerFunction) { return add(RequestPredicates.OPTIONS(pattern), handlerFunction); @@ -141,6 +224,8 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return add(RequestPredicates.OPTIONS(pattern).and(predicate), handlerFunction); } + // other + @Override public RouterFunctions.Builder route(RequestPredicate predicate, HandlerFunction handlerFunction) { 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 8106092cbc..f9f1d92bcf 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 @@ -276,6 +276,7 @@ public abstract class RouterFunctions { return routerFunction; } + /** * Represents a discoverable builder for router functions. * Obtained via {@link RouterFunctions#route()}. @@ -283,6 +284,14 @@ public abstract class RouterFunctions { */ public interface Builder { + /** + * Adds a route to the given handler function that handles HTTP {@code GET} requests. + * @param handlerFunction the handler function to handle all {@code GET} requests + * @return this builder + * @since 5.3 + */ + Builder GET(HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code GET} requests * that match the given pattern. @@ -293,6 +302,18 @@ public abstract class RouterFunctions { */ Builder GET(String pattern, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles all HTTP {@code GET} requests + * that match the given predicate. + * @param predicate predicate to match + * @param handlerFunction the handler function to handle all {@code GET} requests that + * match {@code predicate} + * @return this builder + * @since 5.3 + * @see RequestPredicates + */ + Builder GET(RequestPredicate predicate, HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code GET} requests * that match the given pattern and predicate. @@ -307,12 +328,20 @@ public abstract class RouterFunctions { * @param pattern the pattern to match to * @param predicate additional predicate to match * @param handlerFunction the handler function to handle all {@code GET} requests that - * match {@code pattern} + * match {@code pattern} and the predicate * @return this builder * @see RequestPredicates */ Builder GET(String pattern, RequestPredicate predicate, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles HTTP {@code HEAD} requests. + * @param handlerFunction the handler function to handle all {@code HEAD} requests + * @return this builder + * @since 5.3 + */ + Builder HEAD(HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code HEAD} requests * that match the given pattern. @@ -323,6 +352,18 @@ public abstract class RouterFunctions { */ Builder HEAD(String pattern, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles all HTTP {@code HEAD} requests + * that match the given predicate. + * @param predicate predicate to match + * @param handlerFunction the handler function to handle all {@code HEAD} requests that + * match {@code predicate} + * @return this builder + * @since 5.3 + * @see RequestPredicates + */ + Builder HEAD(RequestPredicate predicate, HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code HEAD} requests * that match the given pattern and predicate. @@ -334,6 +375,14 @@ public abstract class RouterFunctions { */ Builder HEAD(String pattern, RequestPredicate predicate, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles HTTP {@code POST} requests. + * @param handlerFunction the handler function to handle all {@code POST} requests + * @return this builder + * @since 5.3 + */ + Builder POST(HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code POST} requests * that match the given pattern. @@ -344,6 +393,18 @@ public abstract class RouterFunctions { */ Builder POST(String pattern, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles all HTTP {@code POST} requests + * that match the given predicate. + * @param predicate predicate to match + * @param handlerFunction the handler function to handle all {@code POST} requests that + * match {@code predicate} + * @return this builder + * @since 5.3 + * @see RequestPredicates + */ + Builder POST(RequestPredicate predicate, HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code POST} requests * that match the given pattern and predicate. @@ -363,6 +424,14 @@ public abstract class RouterFunctions { */ Builder POST(String pattern, RequestPredicate predicate, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles HTTP {@code PUT} requests. + * @param handlerFunction the handler function to handle all {@code PUT} requests + * @return this builder + * @since 5.3 + */ + Builder PUT(HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code PUT} requests * that match the given pattern. @@ -373,6 +442,18 @@ public abstract class RouterFunctions { */ Builder PUT(String pattern, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles all HTTP {@code PUT} requests + * that match the given predicate. + * @param predicate predicate to match + * @param handlerFunction the handler function to handle all {@code PUT} requests that + * match {@code predicate} + * @return this builder + * @since 5.3 + * @see RequestPredicates + */ + Builder PUT(RequestPredicate predicate, HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code PUT} requests * that match the given pattern and predicate. @@ -392,6 +473,14 @@ public abstract class RouterFunctions { */ Builder PUT(String pattern, RequestPredicate predicate, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles HTTP {@code PATCH} requests. + * @param handlerFunction the handler function to handle all {@code PATCH} requests + * @return this builder + * @since 5.3 + */ + Builder PATCH(HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code PATCH} requests * that match the given pattern. @@ -402,6 +491,18 @@ public abstract class RouterFunctions { */ Builder PATCH(String pattern, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles all HTTP {@code PATCH} requests + * that match the given predicate. + * @param predicate predicate to match + * @param handlerFunction the handler function to handle all {@code PATCH} requests that + * match {@code predicate} + * @return this builder + * @since 5.3 + * @see RequestPredicates + */ + Builder PATCH(RequestPredicate predicate, HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code PATCH} requests * that match the given pattern and predicate. @@ -421,6 +522,14 @@ public abstract class RouterFunctions { */ Builder PATCH(String pattern, RequestPredicate predicate, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles HTTP {@code DELETE} requests. + * @param handlerFunction the handler function to handle all {@code DELETE} requests + * @return this builder + * @since 5.3 + */ + Builder DELETE(HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code DELETE} requests * that match the given pattern. @@ -431,6 +540,18 @@ public abstract class RouterFunctions { */ Builder DELETE(String pattern, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles all HTTP {@code DELETE} requests + * that match the given predicate. + * @param predicate predicate to match + * @param handlerFunction the handler function to handle all {@code DELETE} requests that + * match {@code predicate} + * @return this builder + * @since 5.3 + * @see RequestPredicates + */ + Builder DELETE(RequestPredicate predicate, HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code DELETE} requests * that match the given pattern and predicate. @@ -442,6 +563,14 @@ public abstract class RouterFunctions { */ Builder DELETE(String pattern, RequestPredicate predicate, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles HTTP {@code OPTIONS} requests. + * @param handlerFunction the handler function to handle all {@code OPTIONS} requests + * @return this builder + * @since 5.3 + */ + Builder OPTIONS(HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code OPTIONS} requests * that match the given pattern. @@ -452,6 +581,18 @@ public abstract class RouterFunctions { */ Builder OPTIONS(String pattern, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles all HTTP {@code OPTIONS} requests + * that match the given predicate. + * @param predicate predicate to match + * @param handlerFunction the handler function to handle all {@code OPTIONS} requests that + * match {@code predicate} + * @return this builder + * @since 5.3 + * @see RequestPredicates + */ + Builder OPTIONS(RequestPredicate predicate, HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code OPTIONS} requests * that match the given pattern and predicate. diff --git a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDsl.kt b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDsl.kt index 2daa78e4c0..5142c59f7f 100644 --- a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDsl.kt +++ b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDsl.kt @@ -58,6 +58,7 @@ fun router(routes: RouterFunctionDsl.() -> Unit) = RouterFunctionDsl(routes).bui * * @author Sebastien Deleuze * @author Yevhenii Melnyk + * @author Arjen Poutsma * @since 5.0 */ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDsl.() -> Unit) { @@ -148,6 +149,14 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs builder.path(this, Supplier { RouterFunctionDsl(init).build() }) } + /** + * Adds a route to the given handler function that handles all HTTP `GET` requests. + * @since 5.3 + */ + fun GET(f: (ServerRequest) -> Mono) { + builder.GET { f(it).cast(ServerResponse::class.java) } + } + /** * Adds a route to the given handler function that handles all HTTP `GET` requests * that match the given pattern. @@ -157,6 +166,16 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs builder.GET(pattern) { f(it).cast(ServerResponse::class.java) } } + /** + * Adds a route to the given handler function that handles all HTTP `GET` requests + * that match the given predicate. + * @param predicate predicate to match + * @since 5.3 + */ + fun GET(predicate: RequestPredicate, f: (ServerRequest) -> Mono) { + builder.GET(predicate, HandlerFunction { f(it).cast(ServerResponse::class.java) }) + } + /** * Adds a route to the given handler function that handles all HTTP `GET` requests * that match the given pattern and predicate. @@ -170,11 +189,19 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs /** * Return a [RequestPredicate] that matches if request's HTTP method is `GET` - * and the given [pattern] matches against the request path. + * and the given `pattern` matches against the request path. * @see RequestPredicates.GET */ fun GET(pattern: String): RequestPredicate = RequestPredicates.GET(pattern) + /** + * Adds a route to the given handler function that handles all HTTP `HEAD` requests. + * @since 5.3 + */ + fun HEAD(f: (ServerRequest) -> Mono) { + builder.HEAD { f(it).cast(ServerResponse::class.java) } + } + /** * Adds a route to the given handler function that handles all HTTP `HEAD` requests * that match the given pattern. @@ -186,7 +213,17 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs /** * Adds a route to the given handler function that handles all HTTP `HEAD` requests - * that match the given pattern. + * that match the given predicate. + * @param predicate predicate to match + * @since 5.3 + */ + fun HEAD(predicate: RequestPredicate, f: (ServerRequest) -> Mono) { + builder.HEAD(predicate, HandlerFunction { f(it).cast(ServerResponse::class.java) }) + } + + /** + * Adds a route to the given handler function that handles all HTTP `HEAD` requests + * that match the given pattern and predicate. * @param pattern the pattern to match to * @param predicate additional predicate to match * @since 5.2 @@ -202,6 +239,14 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs */ fun HEAD(pattern: String): RequestPredicate = RequestPredicates.HEAD(pattern) + /** + * Adds a route to the given handler function that handles all HTTP `POST` requests. + * @since 5.3 + */ + fun POST(f: (ServerRequest) -> Mono) { + builder.POST { f(it).cast(ServerResponse::class.java) } + } + /** * Adds a route to the given handler function that handles all HTTP `POST` requests * that match the given pattern. @@ -213,7 +258,17 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs /** * Adds a route to the given handler function that handles all HTTP `POST` requests - * that match the given pattern. + * that match the given predicate. + * @param predicate predicate to match + * @since 5.3 + */ + fun POST(predicate: RequestPredicate, f: (ServerRequest) -> Mono) { + builder.POST(predicate, HandlerFunction { f(it).cast(ServerResponse::class.java) }) + } + + /** + * Adds a route to the given handler function that handles all HTTP `POST` requests + * that match the given pattern and predicate. * @param pattern the pattern to match to * @param predicate additional predicate to match * @since 5.2 @@ -229,6 +284,14 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs */ fun POST(pattern: String): RequestPredicate = RequestPredicates.POST(pattern) + /** + * Adds a route to the given handler function that handles all HTTP `PUT` requests. + * @since 5.3 + */ + fun PUT(f: (ServerRequest) -> Mono) { + builder.PUT { f(it).cast(ServerResponse::class.java) } + } + /** * Adds a route to the given handler function that handles all HTTP `PUT` requests * that match the given pattern. @@ -240,7 +303,17 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs /** * Adds a route to the given handler function that handles all HTTP `PUT` requests - * that match the given pattern. + * that match the given predicate. + * @param predicate predicate to match + * @since 5.3 + */ + fun PUT(predicate: RequestPredicate, f: (ServerRequest) -> Mono) { + builder.PUT(predicate, HandlerFunction { f(it).cast(ServerResponse::class.java) }) + } + + /** + * Adds a route to the given handler function that handles all HTTP `PUT` requests + * that match the given pattern and predicate. * @param pattern the pattern to match to * @param predicate additional predicate to match * @since 5.2 @@ -256,15 +329,33 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs */ fun PUT(pattern: String): RequestPredicate = RequestPredicates.PUT(pattern) + /** + * Adds a route to the given handler function that handles all HTTP `PATCH` requests. + * @since 5.3 + */ + fun PATCH(f: (ServerRequest) -> Mono) { + builder.PATCH { f(it).cast(ServerResponse::class.java) } + } + /** * Adds a route to the given handler function that handles all HTTP `PATCH` requests - * that match the given pattern and predicate. + * that match the given pattern. * @param pattern the pattern to match to */ fun PATCH(pattern: String, f: (ServerRequest) -> Mono) { builder.PATCH(pattern) { f(it).cast(ServerResponse::class.java) } } + /** + * Adds a route to the given handler function that handles all HTTP `PATCH` requests + * that match the given predicate. + * @param predicate predicate to match + * @since 5.3 + */ + fun PATCH(predicate: RequestPredicate, f: (ServerRequest) -> Mono) { + builder.PATCH(predicate, HandlerFunction { f(it).cast(ServerResponse::class.java) }) + } + /** * Adds a route to the given handler function that handles all HTTP `PATCH` requests * that match the given pattern and predicate. @@ -280,11 +371,19 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs * Return a [RequestPredicate] that matches if request's HTTP method is `PATCH` * and the given `pattern` matches against the request path. * @param pattern the path pattern to match against - * @return a predicate that matches if the request method is PATCH and if the given pattern + * @return a predicate that matches if the request method is `PATCH` and if the given pattern * matches against the request path */ fun PATCH(pattern: String): RequestPredicate = RequestPredicates.PATCH(pattern) + /** + * Adds a route to the given handler function that handles all HTTP `DELETE` requests. + * @since 5.3 + */ + fun DELETE(f: (ServerRequest) -> Mono) { + builder.DELETE { f(it).cast(ServerResponse::class.java) } + } + /** * Adds a route to the given handler function that handles all HTTP `DELETE` requests * that match the given pattern. @@ -296,7 +395,17 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs /** * Adds a route to the given handler function that handles all HTTP `DELETE` requests - * that match the given pattern. + * that match the given predicate. + * @param predicate predicate to match + * @since 5.3 + */ + fun DELETE(predicate: RequestPredicate, f: (ServerRequest) -> Mono) { + builder.DELETE(predicate, HandlerFunction { f(it).cast(ServerResponse::class.java) }) + } + + /** + * Adds a route to the given handler function that handles all HTTP `DELETE` requests + * that match the given pattern and predicate. * @param pattern the pattern to match to * @param predicate additional predicate to match * @since 5.2 @@ -309,11 +418,19 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs * Return a [RequestPredicate] that matches if request's HTTP method is `DELETE` * and the given `pattern` matches against the request path. * @param pattern the path pattern to match against - * @return a predicate that matches if the request method is DELETE and if the given pattern + * @return a predicate that matches if the request method is `DELETE` and if the given pattern * matches against the request path */ fun DELETE(pattern: String): RequestPredicate = RequestPredicates.DELETE(pattern) + /** + * Adds a route to the given handler function that handles all HTTP `OPTIONS` requests. + * @since 5.3 + */ + fun OPTIONS(f: (ServerRequest) -> Mono) { + builder.OPTIONS { f(it).cast(ServerResponse::class.java) } + } + /** * Adds a route to the given handler function that handles all HTTP `OPTIONS` requests * that match the given pattern. @@ -325,7 +442,17 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs /** * Adds a route to the given handler function that handles all HTTP `OPTIONS` requests - * that match the given pattern. + * that match the given predicate. + * @param predicate predicate to match + * @since 5.3 + */ + fun OPTIONS(predicate: RequestPredicate, f: (ServerRequest) -> Mono) { + builder.OPTIONS(predicate, HandlerFunction { f(it).cast(ServerResponse::class.java) }) + } + + /** + * Adds a route to the given handler function that handles all HTTP `OPTIONS` requests + * that match the given pattern and predicate. * @param pattern the pattern to match to * @param predicate additional predicate to match * @since 5.2 @@ -338,7 +465,7 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs * Return a [RequestPredicate] that matches if request's HTTP method is `OPTIONS` * and the given `pattern` matches against the request path. * @param pattern the path pattern to match against - * @return a predicate that matches if the request method is OPTIONS and if the given pattern + * @return a predicate that matches if the request method is `OPTIONS` and if the given pattern * matches against the request path */ fun OPTIONS(pattern: String): RequestPredicate = RequestPredicates.OPTIONS(pattern) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RouterFunctionBuilder.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RouterFunctionBuilder.java index 37a06fe972..49af7797c8 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RouterFunctionBuilder.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RouterFunctionBuilder.java @@ -26,6 +26,7 @@ import java.util.function.Predicate; import java.util.function.Supplier; import org.springframework.core.io.Resource; +import org.springframework.http.HttpMethod; import org.springframework.util.Assert; /** @@ -36,9 +37,9 @@ import org.springframework.util.Assert; */ class RouterFunctionBuilder implements RouterFunctions.Builder { - private List> routerFunctions = new ArrayList<>(); + private final List> routerFunctions = new ArrayList<>(); - private List> filterFunctions = new ArrayList<>(); + private final List> filterFunctions = new ArrayList<>(); @Override @@ -48,13 +49,23 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return this; } - private RouterFunctions.Builder add(RequestPredicate predicate, - HandlerFunction handlerFunction) { - + private RouterFunctions.Builder add(RequestPredicate predicate, HandlerFunction handlerFunction) { this.routerFunctions.add(RouterFunctions.route(predicate, handlerFunction)); return this; } + // GET + + @Override + public RouterFunctions.Builder GET(HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.GET), handlerFunction); + } + + @Override + public RouterFunctions.Builder GET(RequestPredicate predicate, HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.GET).and(predicate), handlerFunction); + } + @Override public RouterFunctions.Builder GET(String pattern, HandlerFunction handlerFunction) { return add(RequestPredicates.GET(pattern), handlerFunction); @@ -67,6 +78,18 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return add(RequestPredicates.GET(pattern).and(predicate), handlerFunction); } + // HEAD + + @Override + public RouterFunctions.Builder HEAD(HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.HEAD), handlerFunction); + } + + @Override + public RouterFunctions.Builder HEAD(RequestPredicate predicate, HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.HEAD).and(predicate), handlerFunction); + } + @Override public RouterFunctions.Builder HEAD(String pattern, HandlerFunction handlerFunction) { return add(RequestPredicates.HEAD(pattern), handlerFunction); @@ -79,6 +102,18 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return add(RequestPredicates.HEAD(pattern).and(predicate), handlerFunction); } + // POST + + @Override + public RouterFunctions.Builder POST(HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.POST), handlerFunction); + } + + @Override + public RouterFunctions.Builder POST(RequestPredicate predicate, HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.POST).and(predicate), handlerFunction); + } + @Override public RouterFunctions.Builder POST(String pattern, HandlerFunction handlerFunction) { return add(RequestPredicates.POST(pattern), handlerFunction); @@ -91,6 +126,18 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return add(RequestPredicates.POST(pattern).and(predicate), handlerFunction); } + // PUT + + @Override + public RouterFunctions.Builder PUT(HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.PUT), handlerFunction); + } + + @Override + public RouterFunctions.Builder PUT(RequestPredicate predicate, HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.PUT).and(predicate), handlerFunction); + } + @Override public RouterFunctions.Builder PUT(String pattern, HandlerFunction handlerFunction) { return add(RequestPredicates.PUT(pattern), handlerFunction); @@ -103,6 +150,18 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return add(RequestPredicates.PUT(pattern).and(predicate), handlerFunction); } + // PATCH + + @Override + public RouterFunctions.Builder PATCH(HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.PATCH), handlerFunction); + } + + @Override + public RouterFunctions.Builder PATCH(RequestPredicate predicate, HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.PATCH).and(predicate), handlerFunction); + } + @Override public RouterFunctions.Builder PATCH(String pattern, HandlerFunction handlerFunction) { return add(RequestPredicates.PATCH(pattern), handlerFunction); @@ -115,6 +174,18 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return add(RequestPredicates.PATCH(pattern).and(predicate), handlerFunction); } + // DELETE + + @Override + public RouterFunctions.Builder DELETE(HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.DELETE), handlerFunction); + } + + @Override + public RouterFunctions.Builder DELETE(RequestPredicate predicate, HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.DELETE).and(predicate), handlerFunction); + } + @Override public RouterFunctions.Builder DELETE(String pattern, HandlerFunction handlerFunction) { return add(RequestPredicates.DELETE(pattern), handlerFunction); @@ -127,15 +198,21 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return add(RequestPredicates.DELETE(pattern).and(predicate), handlerFunction); } + // OPTIONS + @Override - public RouterFunctions.Builder OPTIONS(String pattern, HandlerFunction handlerFunction) { - return add(RequestPredicates.OPTIONS(pattern), handlerFunction); + public RouterFunctions.Builder OPTIONS(HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.OPTIONS), handlerFunction); } @Override - public RouterFunctions.Builder route(RequestPredicate predicate, - HandlerFunction handlerFunction) { - return add(RouterFunctions.route(predicate, handlerFunction)); + public RouterFunctions.Builder OPTIONS(RequestPredicate predicate, HandlerFunction handlerFunction) { + return add(RequestPredicates.method(HttpMethod.OPTIONS).and(predicate), handlerFunction); + } + + @Override + public RouterFunctions.Builder OPTIONS(String pattern, HandlerFunction handlerFunction) { + return add(RequestPredicates.OPTIONS(pattern), handlerFunction); } @Override @@ -145,6 +222,14 @@ class RouterFunctionBuilder implements RouterFunctions.Builder { return add(RequestPredicates.OPTIONS(pattern).and(predicate), handlerFunction); } + // other + + @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-webmvc/src/main/java/org/springframework/web/servlet/function/RouterFunctions.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RouterFunctions.java index 8a9904b191..07a005c9c5 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RouterFunctions.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RouterFunctions.java @@ -196,6 +196,14 @@ public abstract class RouterFunctions { */ public interface Builder { + /** + * Adds a route to the given handler function that handles HTTP {@code GET} requests. + * @param handlerFunction the handler function to handle all {@code GET} requests + * @return this builder + * @since 5.3 + */ + Builder GET(HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code GET} requests * that match the given pattern. @@ -206,6 +214,18 @@ public abstract class RouterFunctions { */ Builder GET(String pattern, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles all HTTP {@code GET} requests + * that match the given predicate. + * @param predicate predicate to match + * @param handlerFunction the handler function to handle all {@code GET} requests that + * match {@code predicate} + * @return this builder + * @since 5.3 + * @see RequestPredicates + */ + Builder GET(RequestPredicate predicate, HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code GET} requests * that match the given pattern and predicate. @@ -220,12 +240,20 @@ public abstract class RouterFunctions { * @param pattern the pattern to match to * @param predicate additional predicate to match * @param handlerFunction the handler function to handle all {@code GET} requests that - * match {@code pattern} + * match {@code pattern} and the predicate * @return this builder * @see RequestPredicates */ Builder GET(String pattern, RequestPredicate predicate, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles HTTP {@code HEAD} requests. + * @param handlerFunction the handler function to handle all {@code HEAD} requests + * @return this builder + * @since 5.3 + */ + Builder HEAD(HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code HEAD} requests * that match the given pattern. @@ -236,6 +264,18 @@ public abstract class RouterFunctions { */ Builder HEAD(String pattern, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles all HTTP {@code HEAD} requests + * that match the given predicate. + * @param predicate predicate to match + * @param handlerFunction the handler function to handle all {@code HEAD} requests that + * match {@code predicate} + * @return this builder + * @since 5.3 + * @see RequestPredicates + */ + Builder HEAD(RequestPredicate predicate, HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code HEAD} requests * that match the given pattern and predicate. @@ -247,6 +287,14 @@ public abstract class RouterFunctions { */ Builder HEAD(String pattern, RequestPredicate predicate, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles HTTP {@code POST} requests. + * @param handlerFunction the handler function to handle all {@code POST} requests + * @return this builder + * @since 5.3 + */ + Builder POST(HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code POST} requests * that match the given pattern. @@ -257,6 +305,18 @@ public abstract class RouterFunctions { */ Builder POST(String pattern, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles all HTTP {@code POST} requests + * that match the given predicate. + * @param predicate predicate to match + * @param handlerFunction the handler function to handle all {@code POST} requests that + * match {@code predicate} + * @return this builder + * @since 5.3 + * @see RequestPredicates + */ + Builder POST(RequestPredicate predicate, HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code POST} requests * that match the given pattern and predicate. @@ -276,6 +336,14 @@ public abstract class RouterFunctions { */ Builder POST(String pattern, RequestPredicate predicate, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles HTTP {@code PUT} requests. + * @param handlerFunction the handler function to handle all {@code PUT} requests + * @return this builder + * @since 5.3 + */ + Builder PUT(HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code PUT} requests * that match the given pattern. @@ -286,6 +354,18 @@ public abstract class RouterFunctions { */ Builder PUT(String pattern, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles all HTTP {@code PUT} requests + * that match the given predicate. + * @param predicate predicate to match + * @param handlerFunction the handler function to handle all {@code PUT} requests that + * match {@code predicate} + * @return this builder + * @since 5.3 + * @see RequestPredicates + */ + Builder PUT(RequestPredicate predicate, HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code PUT} requests * that match the given pattern and predicate. @@ -305,6 +385,14 @@ public abstract class RouterFunctions { */ Builder PUT(String pattern, RequestPredicate predicate, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles HTTP {@code PATCH} requests. + * @param handlerFunction the handler function to handle all {@code PATCH} requests + * @return this builder + * @since 5.3 + */ + Builder PATCH(HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code PATCH} requests * that match the given pattern. @@ -315,6 +403,18 @@ public abstract class RouterFunctions { */ Builder PATCH(String pattern, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles all HTTP {@code PATCH} requests + * that match the given predicate. + * @param predicate predicate to match + * @param handlerFunction the handler function to handle all {@code PATCH} requests that + * match {@code predicate} + * @return this builder + * @since 5.3 + * @see RequestPredicates + */ + Builder PATCH(RequestPredicate predicate, HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code PATCH} requests * that match the given pattern and predicate. @@ -334,6 +434,14 @@ public abstract class RouterFunctions { */ Builder PATCH(String pattern, RequestPredicate predicate, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles HTTP {@code DELETE} requests. + * @param handlerFunction the handler function to handle all {@code DELETE} requests + * @return this builder + * @since 5.3 + */ + Builder DELETE(HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code DELETE} requests * that match the given pattern. @@ -344,6 +452,18 @@ public abstract class RouterFunctions { */ Builder DELETE(String pattern, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles all HTTP {@code DELETE} requests + * that match the given predicate. + * @param predicate predicate to match + * @param handlerFunction the handler function to handle all {@code DELETE} requests that + * match {@code predicate} + * @return this builder + * @since 5.3 + * @see RequestPredicates + */ + Builder DELETE(RequestPredicate predicate, HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code DELETE} requests * that match the given pattern and predicate. @@ -355,6 +475,14 @@ public abstract class RouterFunctions { */ Builder DELETE(String pattern, RequestPredicate predicate, HandlerFunction handlerFunction); + /** + * Adds a route to the given handler function that handles HTTP {@code OPTIONS} requests. + * @param handlerFunction the handler function to handle all {@code OPTIONS} requests + * @return this builder + * @since 5.3 + */ + Builder OPTIONS(HandlerFunction handlerFunction); + /** * Adds a route to the given handler function that handles all HTTP {@code OPTIONS} requests * that match the given pattern. @@ -366,15 +494,16 @@ public abstract class RouterFunctions { Builder OPTIONS(String pattern, 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 + * Adds a route to the given handler function that handles all HTTP {@code OPTIONS} requests + * that match the given predicate. + * @param predicate predicate to match + * @param handlerFunction the handler function to handle all {@code OPTIONS} requests that + * match {@code predicate} * @return this builder + * @since 5.3 * @see RequestPredicates */ - Builder route(RequestPredicate predicate, HandlerFunction handlerFunction); + Builder OPTIONS(RequestPredicate predicate, HandlerFunction handlerFunction); /** * Adds a route to the given handler function that handles all HTTP {@code OPTIONS} requests @@ -387,6 +516,16 @@ 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 + * @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-webmvc/src/main/kotlin/org/springframework/web/servlet/function/RouterFunctionDsl.kt b/spring-webmvc/src/main/kotlin/org/springframework/web/servlet/function/RouterFunctionDsl.kt index 56b6af2c8e..6866167673 100644 --- a/spring-webmvc/src/main/kotlin/org/springframework/web/servlet/function/RouterFunctionDsl.kt +++ b/spring-webmvc/src/main/kotlin/org/springframework/web/servlet/function/RouterFunctionDsl.kt @@ -48,6 +48,7 @@ import java.util.function.Supplier * } * ``` * @author Sebastien Deleuze + * @author Arjen Poutsma * @since 5.2 */ fun router(routes: (RouterFunctionDsl.() -> Unit)) = RouterFunctionDsl(routes).build() @@ -134,7 +135,6 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD builder.nest(this, Supplier(RouterFunctionDsl(r)::build)) } - /** * Route to the given router function if the given request predicate (String * processed as a path predicate) applies. This method can be used to create @@ -145,6 +145,14 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD */ fun String.nest(r: (RouterFunctionDsl.() -> Unit)) = path(this).nest(r) + /** + * Adds a route to the given handler function that handles all HTTP `GET` requests. + * @since 5.3 + */ + fun GET(f: (ServerRequest) -> ServerResponse) { + builder.GET(HandlerFunction(f)) + } + /** * Adds a route to the given handler function that handles all HTTP `GET` requests * that match the given pattern. @@ -156,7 +164,17 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD /** * Adds a route to the given handler function that handles all HTTP `GET` requests - * that match the given pattern. + * that match the given predicate. + * @param predicate predicate to match + * @since 5.3 + */ + fun GET(predicate: RequestPredicate, f: (ServerRequest) -> ServerResponse) { + builder.GET(predicate, HandlerFunction(f)) + } + + /** + * Adds a route to the given handler function that handles all HTTP `GET` requests + * that match the given pattern and predicate. * @param pattern the pattern to match to * @param predicate additional predicate to match * @since 5.2 @@ -172,6 +190,14 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD */ fun GET(pattern: String): RequestPredicate = RequestPredicates.GET(pattern) + /** + * Adds a route to the given handler function that handles all HTTP `HEAD` requests. + * @since 5.3 + */ + fun HEAD(f: (ServerRequest) -> ServerResponse) { + builder.HEAD(HandlerFunction(f)) + } + /** * Adds a route to the given handler function that handles all HTTP `HEAD` requests * that match the given pattern. @@ -183,7 +209,17 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD /** * Adds a route to the given handler function that handles all HTTP `HEAD` requests - * that match the given pattern. + * that match the given predicate. + * @param predicate predicate to match + * @since 5.3 + */ + fun HEAD(predicate: RequestPredicate, f: (ServerRequest) -> ServerResponse) { + builder.HEAD(predicate, HandlerFunction(f)) + } + + /** + * Adds a route to the given handler function that handles all HTTP `HEAD` requests + * that match the given pattern and predicate. * @param pattern the pattern to match to * @param predicate additional predicate to match * @since 5.2 @@ -199,6 +235,14 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD */ fun HEAD(pattern: String): RequestPredicate = RequestPredicates.HEAD(pattern) + /** + * Adds a route to the given handler function that handles all HTTP `POST` requests. + * @since 5.3 + */ + fun POST(f: (ServerRequest) -> ServerResponse) { + builder.POST(HandlerFunction(f)) + } + /** * Adds a route to the given handler function that handles all HTTP `POST` requests * that match the given pattern. @@ -210,7 +254,17 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD /** * Adds a route to the given handler function that handles all HTTP `POST` requests - * that match the given pattern. + * that match the given predicate. + * @param predicate predicate to match + * @since 5.3 + */ + fun POST(predicate: RequestPredicate, f: (ServerRequest) -> ServerResponse) { + builder.POST(predicate, HandlerFunction(f)) + } + + /** + * Adds a route to the given handler function that handles all HTTP `POST` requests + * that match the given pattern and predicate. * @param pattern the pattern to match to * @param predicate additional predicate to match * @since 5.2 @@ -226,6 +280,14 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD */ fun POST(pattern: String): RequestPredicate = RequestPredicates.POST(pattern) + /** + * Adds a route to the given handler function that handles all HTTP `PUT` requests. + * @since 5.3 + */ + fun PUT(f: (ServerRequest) -> ServerResponse) { + builder.PUT(HandlerFunction(f)) + } + /** * Adds a route to the given handler function that handles all HTTP `PUT` requests * that match the given pattern. @@ -237,7 +299,17 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD /** * Adds a route to the given handler function that handles all HTTP `PUT` requests - * that match the given pattern. + * that match the given predicate. + * @param predicate predicate to match + * @since 5.3 + */ + fun PUT(predicate: RequestPredicate, f: (ServerRequest) -> ServerResponse) { + builder.PUT(predicate, HandlerFunction(f)) + } + + /** + * Adds a route to the given handler function that handles all HTTP `PUT` requests + * that match the given pattern and predicate. * @param pattern the pattern to match to * @param predicate additional predicate to match * @since 5.2 @@ -253,6 +325,14 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD */ fun PUT(pattern: String): RequestPredicate = RequestPredicates.PUT(pattern) + /** + * Adds a route to the given handler function that handles all HTTP `PATCH` requests. + * @since 5.3 + */ + fun PATCH(f: (ServerRequest) -> ServerResponse) { + builder.PATCH(HandlerFunction(f)) + } + /** * Adds a route to the given handler function that handles all HTTP `PATCH` requests * that match the given pattern. @@ -264,7 +344,17 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD /** * Adds a route to the given handler function that handles all HTTP `PATCH` requests - * that match the given pattern. + * that match the given predicate. + * @param predicate predicate to match + * @since 5.3 + */ + fun PATCH(predicate: RequestPredicate, f: (ServerRequest) -> ServerResponse) { + builder.PATCH(predicate, HandlerFunction(f)) + } + + /** + * Adds a route to the given handler function that handles all HTTP `PATCH` requests + * that match the given pattern and predicate. * @param pattern the pattern to match to * @param predicate additional predicate to match * @since 5.2 @@ -282,6 +372,14 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD */ fun PATCH(pattern: String): RequestPredicate = RequestPredicates.PATCH(pattern) + /** + * Adds a route to the given handler function that handles all HTTP `DELETE` requests. + * @since 5.3 + */ + fun DELETE(f: (ServerRequest) -> ServerResponse) { + builder.DELETE(HandlerFunction(f)) + } + /** * Adds a route to the given handler function that handles all HTTP `DELETE` requests * that match the given pattern. @@ -293,7 +391,17 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD /** * Adds a route to the given handler function that handles all HTTP `DELETE` requests - * that match the given pattern. + * that match the given predicate. + * @param predicate predicate to match + * @since 5.3 + */ + fun DELETE(predicate: RequestPredicate, f: (ServerRequest) -> ServerResponse) { + builder.DELETE(predicate, HandlerFunction(f)) + } + + /** + * Adds a route to the given handler function that handles all HTTP `DELETE` requests + * that match the given pattern and predicate. * @param pattern the pattern to match to * @param predicate additional predicate to match * @since 5.2 @@ -311,6 +419,14 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD */ fun DELETE(pattern: String): RequestPredicate = RequestPredicates.DELETE(pattern) + /** + * Adds a route to the given handler function that handles all HTTP `OPTIONS` requests. + * @since 5.3 + */ + fun OPTIONS(f: (ServerRequest) -> ServerResponse) { + builder.OPTIONS(HandlerFunction(f)) + } + /** * Adds a route to the given handler function that handles all HTTP `OPTIONS` requests * that match the given pattern. @@ -322,7 +438,17 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD /** * Adds a route to the given handler function that handles all HTTP `OPTIONS` requests - * that match the given pattern. + * that match the given predicate. + * @param predicate predicate to match + * @since 5.3 + */ + fun OPTIONS(predicate: RequestPredicate, f: (ServerRequest) -> ServerResponse) { + builder.OPTIONS(predicate, HandlerFunction(f)) + } + + /** + * Adds a route to the given handler function that handles all HTTP `OPTIONS` requests + * that match the given pattern and predicate. * @param pattern the pattern to match to * @param predicate additional predicate to match * @since 5.2 @@ -654,4 +780,4 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD * Equivalent to [RouterFunction.and]. */ operator fun RouterFunction.plus(other: RouterFunction) = - this.and(other) \ No newline at end of file + this.and(other) diff --git a/src/docs/asciidoc/web/webflux-functional.adoc b/src/docs/asciidoc/web/webflux-functional.adoc index bbd597fccd..cc52408082 100644 --- a/src/docs/asciidoc/web/webflux-functional.adoc +++ b/src/docs/asciidoc/web/webflux-functional.adoc @@ -587,7 +587,7 @@ improved in the following way by using nested routes: RouterFunction route = route() .path("/person", builder -> builder // <1> .GET("/{id}", accept(APPLICATION_JSON), handler::getPerson) - .GET("", accept(APPLICATION_JSON), handler::listPeople) + .GET(accept(APPLICATION_JSON), handler::listPeople) .POST("/person", handler::createPerson)) .build(); ---- @@ -599,7 +599,7 @@ RouterFunction route = route() val route = coRouter { "/person".nest { GET("/{id}", accept(APPLICATION_JSON), handler::getPerson) - GET("", accept(APPLICATION_JSON), handler::listPeople) + GET(accept(APPLICATION_JSON), handler::listPeople) POST("/person", handler::createPerson) } } @@ -617,7 +617,7 @@ We can further improve by using the `nest` method together with `accept`: .path("/person", b1 -> b1 .nest(accept(APPLICATION_JSON), b2 -> b2 .GET("/{id}", handler::getPerson) - .GET("", handler::listPeople)) + .GET(handler::listPeople)) .POST("/person", handler::createPerson)) .build(); ---- @@ -628,7 +628,7 @@ We can further improve by using the `nest` method together with `accept`: "/person".nest { accept(APPLICATION_JSON).nest { GET("/{id}", handler::getPerson) - GET("", handler::listPeople) + GET(handler::listPeople) POST("/person", handler::createPerson) } } @@ -760,7 +760,7 @@ For instance, consider the following example: .path("/person", b1 -> b1 .nest(accept(APPLICATION_JSON), b2 -> b2 .GET("/{id}", handler::getPerson) - .GET("", handler::listPeople) + .GET(handler::listPeople) .before(request -> ServerRequest.from(request) // <1> .header("X-RequestHeader", "Value") .build())) @@ -812,7 +812,7 @@ The following example shows how to do so: .path("/person", b1 -> b1 .nest(accept(APPLICATION_JSON), b2 -> b2 .GET("/{id}", handler::getPerson) - .GET("", handler::listPeople)) + .GET(handler::listPeople)) .POST("/person", handler::createPerson)) .filter((request, next) -> { if (securityManager.allowAccessTo(request.path())) { diff --git a/src/docs/asciidoc/web/webmvc-functional.adoc b/src/docs/asciidoc/web/webmvc-functional.adoc index 56fcd20a11..db480cc3fb 100644 --- a/src/docs/asciidoc/web/webmvc-functional.adoc +++ b/src/docs/asciidoc/web/webmvc-functional.adoc @@ -555,7 +555,7 @@ For instance, the last few lines of the example above can be improved in the fol RouterFunction route = route() .path("/person", builder -> builder // <1> .GET("/{id}", accept(APPLICATION_JSON), handler::getPerson) - .GET("", accept(APPLICATION_JSON), handler::listPeople) + .GET(accept(APPLICATION_JSON), handler::listPeople) .POST("/person", handler::createPerson)) .build(); ---- @@ -569,7 +569,7 @@ RouterFunction route = route() val route = router { "/person".nest { GET("/{id}", accept(APPLICATION_JSON), handler::getPerson) - GET("", accept(APPLICATION_JSON), handler::listPeople) + GET(accept(APPLICATION_JSON), handler::listPeople) POST("/person", handler::createPerson) } } @@ -587,7 +587,7 @@ We can further improve by using the `nest` method together with `accept`: .path("/person", b1 -> b1 .nest(accept(APPLICATION_JSON), b2 -> b2 .GET("/{id}", handler::getPerson) - .GET("", handler::listPeople)) + .GET(handler::listPeople)) .POST("/person", handler::createPerson)) .build(); ---- @@ -719,7 +719,7 @@ For instance, consider the following example: .path("/person", b1 -> b1 .nest(accept(APPLICATION_JSON), b2 -> b2 .GET("/{id}", handler::getPerson) - .GET("", handler::listPeople) + .GET(handler::listPeople) .before(request -> ServerRequest.from(request) // <1> .header("X-RequestHeader", "Value") .build())) @@ -738,7 +738,7 @@ For instance, consider the following example: val route = router { "/person".nest { GET("/{id}", handler::getPerson) - GET("", handler::listPeople) + GET(handler::listPeople) before { // <1> ServerRequest.from(it) .header("X-RequestHeader", "Value").build() @@ -773,7 +773,7 @@ The following example shows how to do so: .path("/person", b1 -> b1 .nest(accept(APPLICATION_JSON), b2 -> b2 .GET("/{id}", handler::getPerson) - .GET("", handler::listPeople)) + .GET(handler::listPeople)) .POST("/person", handler::createPerson)) .filter((request, next) -> { if (securityManager.allowAccessTo(request.path())) {