List of usage examples for org.springframework.core ReactiveAdapter toPublisher
@SuppressWarnings("unchecked") public <T> Publisher<T> toPublisher(@Nullable Object source)
From source file:org.springframework.messaging.handler.invocation.reactive.AbstractEncoderMethodReturnValueHandler.java
@SuppressWarnings("unchecked") private Flux<DataBuffer> encodeContent(@Nullable Object content, MethodParameter returnType, DataBufferFactory bufferFactory, @Nullable MimeType mimeType, Map<String, Object> hints) { ResolvableType returnValueType = ResolvableType.forMethodParameter(returnType); ReactiveAdapter adapter = getAdapterRegistry().getAdapter(returnValueType.resolve(), content); Publisher<?> publisher;/*from w w w. java2s .c om*/ ResolvableType elementType; if (adapter != null) { publisher = adapter.toPublisher(content); boolean isUnwrapped = KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(returnType.getContainingClass()) && KotlinDelegate.isSuspend(returnType.getMethod()) && !COROUTINES_FLOW_CLASS_NAME.equals(returnValueType.toClass().getName()); ResolvableType genericType = isUnwrapped ? returnValueType : returnValueType.getGeneric(); elementType = getElementType(adapter, genericType); } else { publisher = Mono.justOrEmpty(content); elementType = (returnValueType.toClass() == Object.class && content != null ? ResolvableType.forInstance(content) : returnValueType); } if (elementType.resolve() == void.class || elementType.resolve() == Void.class) { return Flux.from(publisher).cast(DataBuffer.class); } Encoder<?> encoder = getEncoder(elementType, mimeType); return Flux.from((Publisher) publisher) .map(value -> encodeValue(value, elementType, encoder, bufferFactory, mimeType, hints)); }
From source file:org.springframework.web.reactive.result.view.AbstractView.java
/** * By default, resolve async attributes supported by the * {@link ReactiveAdapterRegistry} to their blocking counterparts. * <p>View implementations capable of taking advantage of reactive types * can override this method if needed./*from w w w .j a v a 2s. com*/ * @return {@code Mono} for the completion of async attributes resolution */ protected Mono<Void> resolveAsyncAttributes(Map<String, Object> model) { List<String> names = new ArrayList<>(); List<Mono<?>> valueMonos = new ArrayList<>(); for (Map.Entry<String, ?> entry : model.entrySet()) { Object value = entry.getValue(); if (value == null) { continue; } ReactiveAdapter adapter = this.adapterRegistry.getAdapter(null, value); if (adapter != null) { names.add(entry.getKey()); if (adapter.isMultiValue()) { Flux<Object> fluxValue = Flux.from(adapter.toPublisher(value)); valueMonos.add(fluxValue.collectList().defaultIfEmpty(Collections.emptyList())); } else { Mono<Object> monoValue = Mono.from(adapter.toPublisher(value)); valueMonos.add(monoValue.defaultIfEmpty(NO_VALUE)); } } } if (names.isEmpty()) { return Mono.empty(); } return Mono.zip(valueMonos, values -> { for (int i = 0; i < values.length; i++) { if (values[i] != NO_VALUE) { model.put(names.get(i), values[i]); } else { model.remove(names.get(i)); } } return NO_VALUE; }).then(); }