spring-framework / org.springframework.web.reactive.function / BodyInserters

BodyInserters

abstract class BodyInserters

Implementations of BodyInserter that write various bodies, such a reactive streams, server-sent events, resources, etc.

Author
Arjen Poutsma

Since
5.0

Constructors

<init>

BodyInserters()

Implementations of BodyInserter that write various bodies, such a reactive streams, server-sent events, resources, etc.

Functions

empty

open static fun <T : Any> empty(): BodyInserter<T, ReactiveHttpOutputMessage>

Return an empty BodyInserter that writes nothing.

fromDataBuffers

open static fun <T : Publisher<DataBuffer>> fromDataBuffers(publisher: T): BodyInserter<T, ReactiveHttpOutputMessage>

Return a BodyInserter that writes the given Publisher<DataBuffer> to the body.

fromFormData

open static fun fromFormData(formData: MultiValueMap<String, String>): FormInserter

Return a FormInserter that writes the given MultiValueMap as URL-encoded form data. The returned inserter allows for additional entries to be added via FormInserter#with(String, Object).

Note that you can also use the syncBody(Object) method in the request builders of both the WebClient and WebTestClient. In that case the setting of the content type is also not required, just be sure the map contains String values only or otherwise it would be interpreted as a multipart request.

open static fun fromFormData(key: String, value: String): FormInserter

Return a FormInserter that writes the given key-value pair as URL-encoded form data. The returned inserter allows for additional entries to be added via FormInserter#with(String, Object).

fromMultipartData

open static fun <T : Any> fromMultipartData(multipartData: MultiValueMap<String, T>): FormInserter

Return a FormInserter that writes the given MultiValueMap as multipart data. The values in the MultiValueMap can be any Object representing the body of the part, or an org.springframework.http.HttpEntity representing a part with body and headers. The MultiValueMap can be built conveniently using org.springframework.http.client.MultipartBodyBuilder. Also the returned inserter allows for additional entries to be added via FormInserter#with(String, Object).

Note that you can also use the syncBody(Object) method in the request builders of both the WebClient and WebTestClient. In that case the setting of the content type is also not required, just be sure the map contains at least one non-String value or otherwise, without a content-type header as a hint, it would be interpreted as a plain form data request.

open static fun <T : Any> fromMultipartData(key: String, value: T): FormInserter

A variant of #fromMultipartData(MultiValueMap) for adding parts as name-value pairs in-line vs building a MultiValueMap and passing it in.

fromObject

open static fun <T : Any> fromObject(body: T): BodyInserter<T, ReactiveHttpOutputMessage>

Return a BodyInserter that writes the given single object.

Note also that org.springframework.web.reactive.function.client.WebClient and org.springframework.web.reactive.function.server.ServerResponse each offer a syncBody(Object) shortcut for providing an Object as the body.

fromPublisher

open static fun <T : Any, P : Publisher<T>> fromPublisher(publisher: P, elementClass: Class<T>): BodyInserter<P, ReactiveHttpOutputMessage>
open static fun <T : Any, P : Publisher<T>> fromPublisher(publisher: P, typeReference: ParameterizedTypeReference<T>): BodyInserter<P, ReactiveHttpOutputMessage>

Return a BodyInserter that writes the given Publisher.

Note also that org.springframework.web.reactive.function.client.WebClient and org.springframework.web.reactive.function.server.ServerResponse each offer body shortcut methods for providing a Publisher as the body.

fromResource

open static fun <T : Resource> fromResource(resource: T): BodyInserter<T, ReactiveHttpOutputMessage>

Return a BodyInserter that writes the given Resource.

If the resource can be resolved to a file, it will be copied using zero-copy.

fromServerSentEvents

open static fun <T : Any, S : Publisher<ServerSentEvent<T>>> fromServerSentEvents(eventsPublisher: S): BodyInserter<S, ServerHttpResponse>

Return a BodyInserter that writes the given ServerSentEvent publisher.

Note that a SSE BodyInserter can also be obtained by passing a stream of strings or POJOs (to be encoded as JSON) to #fromPublisher(Publisher, Class), and specifying a text/event-stream Content-Type.