Skip to content

Commit 8de5c05

Browse files
committed
Set BEST_MATCHING_HANDLER_ATTRIBUTE in WebFlux.fn
SPR-17367
1 parent f12c28e commit 8de5c05

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/RouterFunctionMapping.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Collections;
2020
import java.util.List;
21+
import java.util.Map;
2122
import java.util.stream.Collectors;
2223

2324
import reactor.core.publisher.Mono;
@@ -27,6 +28,7 @@
2728
import org.springframework.http.codec.ServerCodecConfigurer;
2829
import org.springframework.lang.Nullable;
2930
import org.springframework.util.CollectionUtils;
31+
import org.springframework.web.reactive.function.server.HandlerFunction;
3032
import org.springframework.web.reactive.function.server.RouterFunction;
3133
import org.springframework.web.reactive.function.server.RouterFunctions;
3234
import org.springframework.web.reactive.function.server.ServerRequest;
@@ -142,12 +144,32 @@ else if (total > 0) {
142144
protected Mono<?> getHandlerInternal(ServerWebExchange exchange) {
143145
if (this.routerFunction != null) {
144146
ServerRequest request = ServerRequest.create(exchange, this.messageReaders);
145-
exchange.getAttributes().put(RouterFunctions.REQUEST_ATTRIBUTE, request);
146-
return this.routerFunction.route(request);
147+
return this.routerFunction.route(request)
148+
.doOnNext(handler -> setAttributes(exchange.getAttributes(), request, handler));
147149
}
148150
else {
149151
return Mono.empty();
150152
}
151153
}
152154

155+
@SuppressWarnings("unchecked")
156+
private void setAttributes(Map<String, Object> attributes, ServerRequest serverRequest,
157+
HandlerFunction<?> handlerFunction) {
158+
159+
attributes.put(RouterFunctions.REQUEST_ATTRIBUTE, serverRequest);
160+
attributes.put(BEST_MATCHING_HANDLER_ATTRIBUTE, handlerFunction);
161+
162+
String matchingPattern =
163+
(String) attributes.get(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE);
164+
if (matchingPattern != null) {
165+
attributes.put(BEST_MATCHING_PATTERN_ATTRIBUTE, matchingPattern);
166+
}
167+
Map<String, String> uriVariables =
168+
(Map<String, String>) attributes
169+
.get(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
170+
if (uriVariables != null) {
171+
attributes.put(URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriVariables);
172+
}
173+
}
174+
153175
}

0 commit comments

Comments
 (0)