diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/RouterFunctionMapping.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/RouterFunctionMapping.java index 90c626fbd0..c219f9941d 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/RouterFunctionMapping.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/RouterFunctionMapping.java @@ -18,6 +18,7 @@ package org.springframework.web.reactive.function.server.support; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import reactor.core.publisher.Mono; @@ -27,6 +28,7 @@ import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.lang.Nullable; import org.springframework.util.CollectionUtils; +import org.springframework.web.reactive.function.server.HandlerFunction; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerRequest; @@ -142,12 +144,32 @@ public class RouterFunctionMapping extends AbstractHandlerMapping implements Ini protected Mono getHandlerInternal(ServerWebExchange exchange) { if (this.routerFunction != null) { ServerRequest request = ServerRequest.create(exchange, this.messageReaders); - exchange.getAttributes().put(RouterFunctions.REQUEST_ATTRIBUTE, request); - return this.routerFunction.route(request); + return this.routerFunction.route(request) + .doOnNext(handler -> setAttributes(exchange.getAttributes(), request, handler)); } else { return Mono.empty(); } } + @SuppressWarnings("unchecked") + private void setAttributes(Map attributes, ServerRequest serverRequest, + HandlerFunction handlerFunction) { + + attributes.put(RouterFunctions.REQUEST_ATTRIBUTE, serverRequest); + attributes.put(BEST_MATCHING_HANDLER_ATTRIBUTE, handlerFunction); + + String matchingPattern = + (String) attributes.get(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE); + if (matchingPattern != null) { + attributes.put(BEST_MATCHING_PATTERN_ATTRIBUTE, matchingPattern); + } + Map uriVariables = + (Map) attributes + .get(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE); + if (uriVariables != null) { + attributes.put(URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriVariables); + } + } + }