private HandlerResultHandler getResultHandler(HandlerResult handlerResult) { for (HandlerResultHandler resultHandler : resultHandlers) { if (resultHandler.supports(handlerResult)) { return resultHandler; } } throw new IllegalStateException( "No HandlerResultHandler for " + handlerResult.getReturnValue()); }
@Override public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) { ResolvableType returnType = result.getReturnType(); MethodParameter bodyType; Mono<?> returnValueMono; Optional<Object> optionalValue = result.getReturnValue(); Class<?> rawClass = returnType.getRawClass(); ReactiveAdapter adapter = getAdapterRegistry().getAdapterFrom(rawClass, optionalValue); if (adapter != null) { returnValueMono = adapter.toMono(optionalValue); bodyType = new MethodParameter(result.getReturnTypeSource()); bodyType.increaseNestingLevel(); bodyType.increaseNestingLevel(); } else { returnValueMono = Mono.justOrEmpty(optionalValue); bodyType = new MethodParameter(result.getReturnTypeSource()); bodyType.increaseNestingLevel(); } return returnValueMono.then( returnValue -> { Assert.isInstanceOf(HttpEntity.class, returnValue); HttpEntity<?> httpEntity = (HttpEntity<?>) returnValue; if (httpEntity instanceof ResponseEntity) { ResponseEntity<?> responseEntity = (ResponseEntity<?>) httpEntity; exchange.getResponse().setStatusCode(responseEntity.getStatusCode()); } HttpHeaders entityHeaders = httpEntity.getHeaders(); HttpHeaders responseHeaders = exchange.getResponse().getHeaders(); if (!entityHeaders.isEmpty()) { entityHeaders .entrySet() .stream() .filter(entry -> !responseHeaders.containsKey(entry.getKey())) .forEach(entry -> responseHeaders.put(entry.getKey(), entry.getValue())); } return writeBody(httpEntity.getBody(), bodyType, exchange); }); }
@Override public boolean supports(HandlerResult result) { Class<?> returnType = result.getReturnType().getRawClass(); if (isSupportedType(returnType)) { return true; } else { ReactiveAdapter adapter = getAdapterRegistry().getAdapterFrom(returnType, result.getReturnValue()); if (adapter != null && !adapter.getDescriptor().isMultiValue() && !adapter.getDescriptor().isNoValue()) { ResolvableType genericType = result.getReturnType().getGeneric(0); return isSupportedType(genericType.getRawClass()); } } return false; }