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 de95ef625f..171c9b3d33 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
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2017 the original author or authors.
+ * Copyright 2002-2018 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.
@@ -50,7 +50,7 @@ import reactor.core.publisher.Mono
* @see RouterFunctionDsl
* @since 5.0
*/
-fun router(routes: RouterFunctionDsl.() -> Unit) = RouterFunctionDsl().apply(routes).router()
+fun router(routes: RouterFunctionDsl.() -> Unit) = RouterFunctionDsl(routes).invoke()
/**
* Provide a [RouterFunction] Kotlin DSL in order to be able to write idiomatic Kotlin code.
@@ -60,7 +60,7 @@ fun router(routes: RouterFunctionDsl.() -> Unit) = RouterFunctionDsl().apply(rou
* @since 5.0
* @see Kotlin issue about supporting ::foo for member functions
*/
-open class RouterFunctionDsl {
+open class RouterFunctionDsl(private val init: RouterFunctionDsl.() -> Unit) : () -> RouterFunction {
private val routes = mutableListOf>()
@@ -131,8 +131,8 @@ open class RouterFunctionDsl {
* common path (prefix), header, or other request predicate.
* @see RouterFunctions.nest
*/
- fun RequestPredicate.nest(r: RouterFunctionDsl.() -> Unit) {
- routes += RouterFunctions.nest(this, RouterFunctionDsl().apply(r).router())
+ fun RequestPredicate.nest(init: RouterFunctionDsl.() -> Unit) {
+ routes += RouterFunctions.nest(this, RouterFunctionDsl(init).invoke())
}
/**
@@ -143,8 +143,8 @@ open class RouterFunctionDsl {
* @see RouterFunctions.nest
* @see RequestPredicates.path
*/
- fun String.nest(r: RouterFunctionDsl.() -> Unit) {
- routes += RouterFunctions.nest(path(this), RouterFunctionDsl().apply(r).router())
+ fun String.nest(init: RouterFunctionDsl.() -> Unit) {
+ routes += RouterFunctions.nest(path(this), RouterFunctionDsl(init).invoke())
}
/**
@@ -423,9 +423,11 @@ open class RouterFunctionDsl {
/**
* Return a composed routing function created from all the registered routes.
+ * @since 5.1
*/
- internal fun router(): RouterFunction {
+ override fun invoke(): RouterFunction {
+ init()
return routes.reduce(RouterFunction::and)
}
-}
+}
\ No newline at end of file