Throw 404 ResponseStatusException when no routes found

This commit makes it possible to customize 404 responses generated by
RouterFunctionWebHandler, by throwing an ResponseStatusException
instead of returning a standard 404 response.

See gh-25358
This commit is contained in:
Ingmar van Dijk
2020-07-06 09:13:07 +02:00
committed by Arjen Poutsma
parent 5649a6f8ef
commit 69df27a99f
2 changed files with 27 additions and 9 deletions

View File

@@ -32,10 +32,12 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.util.Assert;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebHandler;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
@@ -1231,9 +1233,6 @@ public abstract class RouterFunctions {
private static class RouterFunctionWebHandler implements WebHandler {
private static final HandlerFunction<ServerResponse> NOT_FOUND_HANDLER =
request -> ServerResponse.notFound().build();
private final HandlerStrategies strategies;
private final RouterFunction<?> routerFunction;
@@ -1249,7 +1248,7 @@ public abstract class RouterFunctions {
ServerRequest request = new DefaultServerRequest(exchange, this.strategies.messageReaders());
addAttributes(exchange, request);
return this.routerFunction.route(request)
.defaultIfEmpty(notFound())
.switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND)))
.flatMap(handlerFunction -> wrapException(() -> handlerFunction.handle(request)))
.flatMap(response -> wrapException(() -> response.writeTo(exchange,
new HandlerStrategiesResponseContext(this.strategies))));
@@ -1261,11 +1260,6 @@ public abstract class RouterFunctions {
attributes.put(REQUEST_ATTRIBUTE, request);
}
@SuppressWarnings("unchecked")
private static <T extends ServerResponse> HandlerFunction<T> notFound() {
return (HandlerFunction<T>) NOT_FOUND_HANDLER;
}
private static <T> Mono<T> wrapException(Supplier<Mono<T>> supplier) {
try {
return supplier.get();