Search in sources :

Example 41 with ReactiveAdapter

use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.

the class BodyInserters method fromProducer.

/**
 * Inserter to write the given producer of value(s) which must be a {@link Publisher}
 * or another producer adaptable to a {@code Publisher} via
 * {@link ReactiveAdapterRegistry}.
 * <p>Alternatively, consider using the {@code body} shortcuts on
 * {@link org.springframework.web.reactive.function.client.WebClient WebClient} and
 * {@link org.springframework.web.reactive.function.server.ServerResponse ServerResponse}.
 * @param <T> the type of the body
 * @param producer the source of body value(s).
 * @param elementTypeRef the type of values to be produced
 * @return the inserter to write a producer
 * @since 5.2
 */
public static <T> BodyInserter<T, ReactiveHttpOutputMessage> fromProducer(T producer, ParameterizedTypeReference<?> elementTypeRef) {
    Assert.notNull(producer, "'producer' must not be null");
    Assert.notNull(elementTypeRef, "'elementTypeRef' must not be null");
    ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(producer.getClass());
    Assert.notNull(adapter, "'producer' type is unknown to ReactiveAdapterRegistry");
    return (message, context) -> writeWithMessageWriters(message, context, producer, ResolvableType.forType(elementTypeRef), adapter);
}
Also used : ReactiveAdapter(org.springframework.core.ReactiveAdapter) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Collectors(java.util.stream.Collectors) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) ServerSentEvent(org.springframework.http.codec.ServerSentEvent) MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) Nullable(org.springframework.lang.Nullable) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Resource(org.springframework.core.io.Resource) Assert(org.springframework.util.Assert) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Aggregations

ReactiveAdapter (org.springframework.core.ReactiveAdapter)41 ResolvableType (org.springframework.core.ResolvableType)16 Mono (reactor.core.publisher.Mono)14 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)13 List (java.util.List)12 Assert (org.springframework.util.Assert)12 MethodParameter (org.springframework.core.MethodParameter)11 Nullable (org.springframework.lang.Nullable)11 MediaType (org.springframework.http.MediaType)10 Map (java.util.Map)8 Publisher (org.reactivestreams.Publisher)8 Flux (reactor.core.publisher.Flux)8 DataBuffer (org.springframework.core.io.buffer.DataBuffer)7 ArrayList (java.util.ArrayList)6 Collections (java.util.Collections)6 BindingContext (org.springframework.web.reactive.BindingContext)6 ServerWebExchange (org.springframework.web.server.ServerWebExchange)6 Collectors (java.util.stream.Collectors)5 HttpEntity (org.springframework.http.HttpEntity)5 HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)5