DispatcherHandler alignment with DispatcherServlet

Issue: SPR-15934
This commit is contained in:
Rossen Stoyanchev 2017-09-22 15:44:12 -04:00
parent 816a58fcf9
commit c28a6357c5
1 changed files with 24 additions and 10 deletions

View File

@ -33,6 +33,7 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebHandler; import org.springframework.web.server.WebHandler;
@ -68,11 +69,14 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
private static final Log logger = LogFactory.getLog(DispatcherHandler.class); private static final Log logger = LogFactory.getLog(DispatcherHandler.class);
private List<HandlerMapping> handlerMappings = Collections.emptyList(); @Nullable
private List<HandlerMapping> handlerMappings;
private List<HandlerAdapter> handlerAdapters = Collections.emptyList(); @Nullable
private List<HandlerAdapter> handlerAdapters;
private List<HandlerResultHandler> resultHandlers = Collections.emptyList(); @Nullable
private List<HandlerResultHandler> resultHandlers;
/** /**
@ -95,8 +99,11 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
* Return all {@link HandlerMapping} beans detected by type in the * Return all {@link HandlerMapping} beans detected by type in the
* {@link #setApplicationContext injected context} and also * {@link #setApplicationContext injected context} and also
* {@link AnnotationAwareOrderComparator#sort(List) sorted}. * {@link AnnotationAwareOrderComparator#sort(List) sorted}.
* @return immutable list with the configured mappings * <p><strong>Note:</strong> This method may return {@code null} if invoked
* prior to {@link #setApplicationContext(ApplicationContext)}.
* @return immutable list with the configured mappings or {@code null}
*/ */
@Nullable
public List<HandlerMapping> getHandlerMappings() { public List<HandlerMapping> getHandlerMappings() {
return this.handlerMappings; return this.handlerMappings;
} }
@ -135,6 +142,9 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
ServerHttpRequest request = exchange.getRequest(); ServerHttpRequest request = exchange.getRequest();
logger.debug("Processing " + request.getMethodValue() + " request for [" + request.getURI() + "]"); logger.debug("Processing " + request.getMethodValue() + " request for [" + request.getURI() + "]");
} }
if (this.handlerMappings == null) {
return Mono.error(HANDLER_NOT_FOUND_EXCEPTION);
}
return Flux.fromIterable(this.handlerMappings) return Flux.fromIterable(this.handlerMappings)
.concatMap(mapping -> mapping.getHandler(exchange)) .concatMap(mapping -> mapping.getHandler(exchange))
.next() .next()
@ -144,9 +154,11 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
} }
private Mono<HandlerResult> invokeHandler(ServerWebExchange exchange, Object handler) { private Mono<HandlerResult> invokeHandler(ServerWebExchange exchange, Object handler) {
for (HandlerAdapter handlerAdapter : this.handlerAdapters) { if (this.handlerAdapters != null) {
if (handlerAdapter.supports(handler)) { for (HandlerAdapter handlerAdapter : this.handlerAdapters) {
return handlerAdapter.handle(exchange, handler); if (handlerAdapter.supports(handler)) {
return handlerAdapter.handle(exchange, handler);
}
} }
} }
return Mono.error(new IllegalStateException("No HandlerAdapter: " + handler)); return Mono.error(new IllegalStateException("No HandlerAdapter: " + handler));
@ -159,9 +171,11 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
} }
private HandlerResultHandler getResultHandler(HandlerResult handlerResult) { private HandlerResultHandler getResultHandler(HandlerResult handlerResult) {
for (HandlerResultHandler resultHandler : this.resultHandlers) { if (this.resultHandlers != null) {
if (resultHandler.supports(handlerResult)) { for (HandlerResultHandler resultHandler : this.resultHandlers) {
return resultHandler; if (resultHandler.supports(handlerResult)) {
return resultHandler;
}
} }
} }
throw new IllegalStateException("No HandlerResultHandler for " + handlerResult.getReturnValue()); throw new IllegalStateException("No HandlerResultHandler for " + handlerResult.getReturnValue());