From b84fe99d07f9cb1261950121e8da7133bc645302 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Fri, 3 Dec 2021 12:00:23 +0100 Subject: [PATCH] Polish "Throw 404 ResponseStatusException when no routes found" See gh-25358 --- .../web/reactive/function/server/RouterFunctions.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java index 8b241f70cd..739fc17a1c 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java @@ -1248,7 +1248,7 @@ public abstract class RouterFunctions { ServerRequest request = new DefaultServerRequest(exchange, this.strategies.messageReaders()); addAttributes(exchange, request); return this.routerFunction.route(request) - .switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND))) + .switchIfEmpty(createNotFoundError()) .flatMap(handlerFunction -> wrapException(() -> handlerFunction.handle(request))) .flatMap(response -> wrapException(() -> response.writeTo(exchange, new HandlerStrategiesResponseContext(this.strategies)))); @@ -1260,6 +1260,11 @@ public abstract class RouterFunctions { attributes.put(REQUEST_ATTRIBUTE, request); } + private Mono createNotFoundError() { + return Mono.defer(() -> Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND, + "No matching router function"))); + } + private static Mono wrapException(Supplier> supplier) { try { return supplier.get();