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 1cee19ecefa..1672c434a89 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 @@ -546,7 +546,12 @@ open class RouterFunctionDsl(private val init: RouterFunctionDsl.() -> Unit) : ( */ override fun invoke(): RouterFunction { init() - return routes.reduce(RouterFunction::and) + return if (routes.isEmpty()) { + RouterFunction { Mono.empty() } + } + else { + routes.reduce(RouterFunction::and) + } } } \ No newline at end of file diff --git a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDslTests.kt b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDslTests.kt index ce32b91b771..9fb5f20ccef 100644 --- a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDslTests.kt +++ b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDslTests.kt @@ -120,6 +120,11 @@ class RouterFunctionDslTests { .verifyComplete() } + @Test + fun emptyRouter() { + router { } + } + private fun sampleRouter() = router { (GET("/foo/") or GET("/foos/")) { req -> handle(req) }