Skip to content

Commit b84fe99

Browse files
committed
Polish "Throw 404 ResponseStatusException when no routes found"
See gh-25358
1 parent 69df27a commit b84fe99

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ public Mono<Void> handle(ServerWebExchange exchange) {
12481248
ServerRequest request = new DefaultServerRequest(exchange, this.strategies.messageReaders());
12491249
addAttributes(exchange, request);
12501250
return this.routerFunction.route(request)
1251-
.switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND)))
1251+
.switchIfEmpty(createNotFoundError())
12521252
.flatMap(handlerFunction -> wrapException(() -> handlerFunction.handle(request)))
12531253
.flatMap(response -> wrapException(() -> response.writeTo(exchange,
12541254
new HandlerStrategiesResponseContext(this.strategies))));
@@ -1260,6 +1260,11 @@ private void addAttributes(ServerWebExchange exchange, ServerRequest request) {
12601260
attributes.put(REQUEST_ATTRIBUTE, request);
12611261
}
12621262

1263+
private <R> Mono<R> createNotFoundError() {
1264+
return Mono.defer(() -> Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND,
1265+
"No matching router function")));
1266+
}
1267+
12631268
private static <T> Mono<T> wrapException(Supplier<Mono<T>> supplier) {
12641269
try {
12651270
return supplier.get();

0 commit comments

Comments
 (0)