@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2019 the original author or authors .
* Copyright 2002 - 2020 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 .
@ -24,6 +24,7 @@ import java.util.function.Function;
@@ -24,6 +24,7 @@ import java.util.function.Function;
import java.util.function.Predicate ;
import java.util.function.Supplier ;
import reactor.core.publisher.Flux ;
import reactor.core.publisher.Mono ;
import org.springframework.core.io.Resource ;
@ -241,9 +242,10 @@ class RouterFunctionBuilder implements RouterFunctions.Builder {
@@ -241,9 +242,10 @@ class RouterFunctionBuilder implements RouterFunctions.Builder {
@Override
public RouterFunction < ServerResponse > build ( ) {
RouterFunction < ServerResponse > result = this . routerFunctions . stream ( )
. reduce ( RouterFunction : : and )
. orElseThrow ( IllegalStateException : : new ) ;
if ( this . routerFunctions . isEmpty ( ) ) {
throw new IllegalStateException ( "No routes registered. Register a route with GET(), POST(), etc." ) ;
}
RouterFunction < ServerResponse > result = new BuiltRouterFunction ( this . routerFunctions ) ;
if ( this . filterFunctions . isEmpty ( ) ) {
return result ;
@ -258,4 +260,30 @@ class RouterFunctionBuilder implements RouterFunctions.Builder {
@@ -258,4 +260,30 @@ class RouterFunctionBuilder implements RouterFunctions.Builder {
}
}
/ * *
* Router function returned by { @link # build ( ) } that simply iterates over the registered routes .
* /
private static class BuiltRouterFunction extends RouterFunctions . AbstractRouterFunction < ServerResponse > {
private List < RouterFunction < ServerResponse > > routerFunctions ;
public BuiltRouterFunction ( List < RouterFunction < ServerResponse > > routerFunctions ) {
Assert . notEmpty ( routerFunctions , "RouterFunctions must not be empty" ) ;
this . routerFunctions = routerFunctions ;
}
@Override
public Mono < HandlerFunction < ServerResponse > > route ( ServerRequest request ) {
return Flux . fromIterable ( this . routerFunctions )
. concatMap ( routerFunction - > routerFunction . route ( request ) )
. next ( ) ;
}
@Override
public void accept ( RouterFunctions . Visitor visitor ) {
this . routerFunctions . forEach ( routerFunction - > routerFunction . accept ( visitor ) ) ;
}
}
}