32
32
import reactor .core .publisher .Mono ;
33
33
34
34
import org .springframework .core .io .Resource ;
35
+ import org .springframework .http .HttpStatus ;
35
36
import org .springframework .http .codec .HttpMessageWriter ;
36
37
import org .springframework .http .server .reactive .HttpHandler ;
37
38
import org .springframework .util .Assert ;
38
39
import org .springframework .web .reactive .result .view .ViewResolver ;
40
+ import org .springframework .web .server .ResponseStatusException ;
39
41
import org .springframework .web .server .ServerWebExchange ;
40
42
import org .springframework .web .server .WebHandler ;
41
43
import org .springframework .web .server .adapter .WebHttpHandlerBuilder ;
@@ -1231,9 +1233,6 @@ public List<ViewResolver> viewResolvers() {
1231
1233
1232
1234
private static class RouterFunctionWebHandler implements WebHandler {
1233
1235
1234
- private static final HandlerFunction <ServerResponse > NOT_FOUND_HANDLER =
1235
- request -> ServerResponse .notFound ().build ();
1236
-
1237
1236
private final HandlerStrategies strategies ;
1238
1237
1239
1238
private final RouterFunction <?> routerFunction ;
@@ -1249,7 +1248,7 @@ public Mono<Void> handle(ServerWebExchange exchange) {
1249
1248
ServerRequest request = new DefaultServerRequest (exchange , this .strategies .messageReaders ());
1250
1249
addAttributes (exchange , request );
1251
1250
return this .routerFunction .route (request )
1252
- .defaultIfEmpty ( notFound ())
1251
+ .switchIfEmpty ( createNotFoundError ())
1253
1252
.flatMap (handlerFunction -> wrapException (() -> handlerFunction .handle (request )))
1254
1253
.flatMap (response -> wrapException (() -> response .writeTo (exchange ,
1255
1254
new HandlerStrategiesResponseContext (this .strategies ))));
@@ -1261,9 +1260,9 @@ private void addAttributes(ServerWebExchange exchange, ServerRequest request) {
1261
1260
attributes .put (REQUEST_ATTRIBUTE , request );
1262
1261
}
1263
1262
1264
- @ SuppressWarnings ( "unchecked" )
1265
- private static < T extends ServerResponse > HandlerFunction < T > notFound () {
1266
- return ( HandlerFunction < T >) NOT_FOUND_HANDLER ;
1263
+ private < R > Mono < R > createNotFoundError () {
1264
+ return Mono . defer (() -> Mono . error ( new ResponseStatusException ( HttpStatus . NOT_FOUND ,
1265
+ "No matching router function" ))) ;
1267
1266
}
1268
1267
1269
1268
private static <T > Mono <T > wrapException (Supplier <Mono <T >> supplier ) {
0 commit comments