Browse Source

Improve 404 "handler not found" handling

Remove handler inserted at the end to generate error in favor of doing
the same using the otherwiseIfEmpty operator.
pull/1111/head
Rossen Stoyanchev 10 years ago
parent
commit
f7d4688b84
  1. 20
      spring-web-reactive/src/main/java/org/springframework/web/reactive/DispatcherHandler.java

20
spring-web-reactive/src/main/java/org/springframework/web/reactive/DispatcherHandler.java

@ -58,6 +58,10 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware { @@ -58,6 +58,10 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
private static final Log logger = LogFactory.getLog(DispatcherHandler.class);
@SuppressWarnings("ThrowableInstanceNeverThrown")
private static final Exception HANDLER_NOT_FOUND_EXCEPTION =
new ResponseStatusException(HttpStatus.NOT_FOUND, "No matching handler");
private List<HandlerMapping> handlerMappings;
@ -78,7 +82,6 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware { @@ -78,7 +82,6 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
context, HandlerMapping.class, true, false);
this.handlerMappings = new ArrayList<>(mappingBeans.values());
this.handlerMappings.add(new NotFoundHandlerMapping());
AnnotationAwareOrderComparator.sort(this.handlerMappings);
Map<String, HandlerAdapter> adapterBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
@ -104,6 +107,7 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware { @@ -104,6 +107,7 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
return Flux.fromIterable(this.handlerMappings)
.concatMap(mapping -> mapping.getHandler(exchange))
.next()
.otherwiseIfEmpty(Mono.error(HANDLER_NOT_FOUND_EXCEPTION))
.then(handler -> invokeHandler(exchange, handler))
.then(result -> handleResult(exchange, result));
}
@ -132,18 +136,4 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware { @@ -132,18 +136,4 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
throw new IllegalStateException("No HandlerResultHandler for " + handlerResult.getReturnValue());
}
private static class NotFoundHandlerMapping implements HandlerMapping {
@SuppressWarnings("ThrowableInstanceNeverThrown")
private static final Exception HANDLER_NOT_FOUND_EXCEPTION =
new ResponseStatusException(HttpStatus.NOT_FOUND, "No matching handler");
@Override
public Mono<Object> getHandler(ServerWebExchange exchange) {
return Mono.error(HANDLER_NOT_FOUND_EXCEPTION);
}
}
}
Loading…
Cancel
Save