Class BodyInserters
BodyInserter
implementations.- Since:
- 5.0
- Author:
- Arjen Poutsma, Rossen Stoyanchev, Sebastien Deleuze
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Extension ofBodyInserter
that allows for adding form data or multipart form data.static interface
Extension ofBodyInserters.FormInserter
that allows for adding asynchronous parts. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> BodyInserter<T,
ReactiveHttpOutputMessage> empty()
Inserter that does not write.static <T extends Publisher<DataBuffer>>
BodyInserter<T,ReactiveHttpOutputMessage> fromDataBuffers
(T publisher) Inserter to write the givenPublisher<DataBuffer>
to the body.static BodyInserters.FormInserter<String>
fromFormData
(String name, String value) Return aBodyInserters.FormInserter
to write the given key-value pair as URL-encoded form data.static BodyInserters.FormInserter<String>
fromFormData
(MultiValueMap<String, String> formData) Return aBodyInserters.FormInserter
to write the givenMultiValueMap
as URL-encoded form data.static <T,
P extends Publisher<T>>
BodyInserters.MultipartInserterfromMultipartAsyncData
(String name, P publisher, Class<T> elementClass) Return aBodyInserters.MultipartInserter
to write the given asynchronous parts, as multipart data.static <T,
P extends Publisher<T>>
BodyInserters.MultipartInserterfromMultipartAsyncData
(String name, P publisher, ParameterizedTypeReference<T> typeReference) Variant offromMultipartAsyncData(String, Publisher, Class)
that accepts aParameterizedTypeReference
for the element type, which allows specifying generic type information.fromMultipartData
(String name, Object value) Return aBodyInserters.MultipartInserter
to write the given parts, as multipart data.fromMultipartData
(MultiValueMap<String, ?> multipartData) Return aBodyInserters.MultipartInserter
to write the givenMultiValueMap
as multipart data.static <T> BodyInserter<T,
ReactiveHttpOutputMessage> fromObject
(T body) Deprecated.static <T extends Publisher<DataBuffer>>
BodyInserter<T,ReactiveHttpOutputMessage> fromOutputStream
(Consumer<OutputStream> outputStreamConsumer, Executor executor) Inserter based on bytes written to aOutputStream
.static <T extends Publisher<DataBuffer>>
BodyInserter<T,ReactiveHttpOutputMessage> fromOutputStream
(Consumer<OutputStream> outputStreamConsumer, Executor executor, int chunkSize) Inserter based on bytes written to aOutputStream
.static <T> BodyInserter<T,
ReactiveHttpOutputMessage> fromProducer
(T producer, Class<?> elementClass) Inserter to write the given producer of value(s) which must be aPublisher
or another producer adaptable to aPublisher
viaReactiveAdapterRegistry
.static <T> BodyInserter<T,
ReactiveHttpOutputMessage> fromProducer
(T producer, ParameterizedTypeReference<?> elementTypeRef) Inserter to write the given producer of value(s) which must be aPublisher
or another producer adaptable to aPublisher
viaReactiveAdapterRegistry
.static <T,
P extends Publisher<T>>
BodyInserter<P,ReactiveHttpOutputMessage> fromPublisher
(P publisher, Class<T> elementClass) Inserter to write the givenPublisher
.static <T,
P extends Publisher<T>>
BodyInserter<P,ReactiveHttpOutputMessage> fromPublisher
(P publisher, ParameterizedTypeReference<T> elementTypeRef) Inserter to write the givenPublisher
.static <T extends Resource>
BodyInserter<T,ReactiveHttpOutputMessage> fromResource
(T resource) Inserter to write the givenResource
.static <T,
S extends Publisher<ServerSentEvent<T>>>
BodyInserter<S,ServerHttpResponse> fromServerSentEvents
(S eventsPublisher) Inserter to write the givenServerSentEvent
publisher.static <T> BodyInserter<T,
ReactiveHttpOutputMessage> fromValue
(T body) Inserter to write the given value.
-
Constructor Details
-
BodyInserters
public BodyInserters()
-
-
Method Details
-
empty
Inserter that does not write.- Returns:
- the inserter
-
fromValue
Inserter to write the given value.Alternatively, consider using the
bodyValue(Object)
shortcuts onWebClient
andServerResponse
.- Type Parameters:
T
- the type of the body- Parameters:
body
- the value to write- Returns:
- the inserter to write a single value
- Throws:
IllegalArgumentException
- ifbody
is aPublisher
or an instance of a type supported byReactiveAdapterRegistry.getSharedInstance()
, for whichfromPublisher(Publisher, Class)
orfromProducer(Object, Class)
should be used.- See Also:
-
fromObject
Deprecated.As of Spring Framework 5.2, in favor offromValue(Object)
Inserter to write the given object.Alternatively, consider using the
bodyValue(Object)
shortcuts onWebClient
andServerResponse
.- Type Parameters:
T
- the type of the body- Parameters:
body
- the body to write to the response- Returns:
- the inserter to write a single object
- Throws:
IllegalArgumentException
- ifbody
is aPublisher
or an instance of a type supported byReactiveAdapterRegistry.getSharedInstance()
, for whichfromPublisher(Publisher, Class)
orfromProducer(Object, Class)
should be used.- See Also:
-
fromProducer
public static <T> BodyInserter<T,ReactiveHttpOutputMessage> fromProducer(T producer, Class<?> elementClass) Inserter to write the given producer of value(s) which must be aPublisher
or another producer adaptable to aPublisher
viaReactiveAdapterRegistry
.Alternatively, consider using the
body
shortcuts onWebClient
andServerResponse
.- Type Parameters:
T
- the type of the body- Parameters:
producer
- the source of body value(s).elementClass
- the class of values to be produced- Returns:
- the inserter to write a producer
- Since:
- 5.2
-
fromProducer
public static <T> BodyInserter<T,ReactiveHttpOutputMessage> fromProducer(T producer, ParameterizedTypeReference<?> elementTypeRef) Inserter to write the given producer of value(s) which must be aPublisher
or another producer adaptable to aPublisher
viaReactiveAdapterRegistry
.Alternatively, consider using the
body
shortcuts onWebClient
andServerResponse
.- Type Parameters:
T
- the type of the body- Parameters:
producer
- the source of body value(s).elementTypeRef
- the type of values to be produced- Returns:
- the inserter to write a producer
- Since:
- 5.2
-
fromPublisher
public static <T,P extends Publisher<T>> BodyInserter<P,ReactiveHttpOutputMessage> fromPublisher(P publisher, Class<T> elementClass) Inserter to write the givenPublisher
.Alternatively, consider using the
body
shortcuts onWebClient
andServerResponse
.- Type Parameters:
T
- the type of the elements contained in the publisherP
- thePublisher
type- Parameters:
publisher
- the publisher to write withelementClass
- the class of elements in the publisher- Returns:
- the inserter to write a
Publisher
-
fromPublisher
public static <T,P extends Publisher<T>> BodyInserter<P,ReactiveHttpOutputMessage> fromPublisher(P publisher, ParameterizedTypeReference<T> elementTypeRef) Inserter to write the givenPublisher
.Alternatively, consider using the
body
shortcuts onWebClient
andServerResponse
.- Type Parameters:
T
- the type of the elements contained in the publisherP
- thePublisher
type- Parameters:
publisher
- the publisher to write withelementTypeRef
- the type of elements contained in the publisher- Returns:
- the inserter to write a
Publisher
-
fromResource
public static <T extends Resource> BodyInserter<T,ReactiveHttpOutputMessage> fromResource(T resource) Inserter to write the givenResource
.If the resource can be resolved to a file, it will be copied using zero-copy.
- Type Parameters:
T
- the type of theResource
- Parameters:
resource
- the resource to write to the output message- Returns:
- the inserter to write a
Publisher
-
fromServerSentEvents
public static <T,S extends Publisher<ServerSentEvent<T>>> BodyInserter<S,ServerHttpResponse> fromServerSentEvents(S eventsPublisher) Inserter to write the givenServerSentEvent
publisher.Alternatively, you can provide event data objects via
fromPublisher(Publisher, Class)
orfromProducer(Object, Class)
, and set the "Content-Type" totext/event-stream
.- Type Parameters:
T
- the type of the data elements in theServerSentEvent
- Parameters:
eventsPublisher
- theServerSentEvent
publisher to write to the response body- Returns:
- the inserter to write a
ServerSentEvent
publisher - See Also:
-
fromFormData
public static BodyInserters.FormInserter<String> fromFormData(MultiValueMap<String, String> formData) Return aBodyInserters.FormInserter
to write the givenMultiValueMap
as URL-encoded form data. The returned inserter allows for additional entries to be added viaBodyInserters.FormInserter.with(String, Object)
.Note that you can also use the
bodyValue(Object)
method in the request builders of both theWebClient
andWebTestClient
. In that case the setting of the request 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.- Parameters:
formData
- the form data to write to the output message- Returns:
- the inserter that allows adding more form data
-
fromFormData
Return aBodyInserters.FormInserter
to write the given key-value pair as URL-encoded form data. The returned inserter allows for additional entries to be added viaBodyInserters.FormInserter.with(String, Object)
.- Parameters:
name
- the key to add to the formvalue
- the value to add to the form- Returns:
- the inserter that allows adding more form data
-
fromMultipartData
public static BodyInserters.MultipartInserter fromMultipartData(MultiValueMap<String, ?> multipartData) Return aBodyInserters.MultipartInserter
to write the givenMultiValueMap
as multipart data. Values in the map can be an Object or anHttpEntity
.Note that you can also build the multipart data externally with
MultipartBodyBuilder
, and pass the resulting map directly to thebodyValue(Object)
shortcut method inWebClient
.- Parameters:
multipartData
- the form data to write to the output message- Returns:
- the inserter that allows adding more parts
- See Also:
-
fromMultipartData
Return aBodyInserters.MultipartInserter
to write the given parts, as multipart data. Values in the map can be an Object or anHttpEntity
.Note that you can also build the multipart data externally with
MultipartBodyBuilder
, and pass the resulting map directly to thebodyValue(Object)
shortcut method inWebClient
.- Parameters:
name
- the part namevalue
- the part value, an Object orHttpEntity
- Returns:
- the inserter that allows adding more parts
-
fromMultipartAsyncData
public static <T,P extends Publisher<T>> BodyInserters.MultipartInserter fromMultipartAsyncData(String name, P publisher, Class<T> elementClass) Return aBodyInserters.MultipartInserter
to write the given asynchronous parts, as multipart data.Note that you can also build the multipart data externally with
MultipartBodyBuilder
, and pass the resulting map directly to thebodyValue(Object)
shortcut method inWebClient
.- Parameters:
name
- the part namepublisher
- the publisher that forms the part valueelementClass
- the class contained in thepublisher
- Returns:
- the inserter that allows adding more parts
-
fromMultipartAsyncData
public static <T,P extends Publisher<T>> BodyInserters.MultipartInserter fromMultipartAsyncData(String name, P publisher, ParameterizedTypeReference<T> typeReference) Variant offromMultipartAsyncData(String, Publisher, Class)
that accepts aParameterizedTypeReference
for the element type, which allows specifying generic type information.Note that you can also build the multipart data externally with
MultipartBodyBuilder
, and pass the resulting map directly to thebodyValue(Object)
shortcut method inWebClient
.- Parameters:
name
- the part namepublisher
- the publisher that forms the part valuetypeReference
- the type contained in thepublisher
- Returns:
- the inserter that allows adding more parts
-
fromDataBuffers
public static <T extends Publisher<DataBuffer>> BodyInserter<T,ReactiveHttpOutputMessage> fromDataBuffers(T publisher) Inserter to write the givenPublisher<DataBuffer>
to the body.- Type Parameters:
T
- the type of the publisher- Parameters:
publisher
- the data buffer publisher to write- Returns:
- the inserter to write directly to the body
- See Also:
-
fromOutputStream
public static <T extends Publisher<DataBuffer>> BodyInserter<T,ReactiveHttpOutputMessage> fromOutputStream(Consumer<OutputStream> outputStreamConsumer, Executor executor) Inserter based on bytes written to aOutputStream
.- Parameters:
outputStreamConsumer
- invoked with anOutputStream
that writes to the output messageexecutor
- used to invoke theoutputStreamHandler
on a separate thread- Returns:
- an inserter that writes what is written to the output stream
- Since:
- 6.1
- See Also:
-
fromOutputStream
public static <T extends Publisher<DataBuffer>> BodyInserter<T,ReactiveHttpOutputMessage> fromOutputStream(Consumer<OutputStream> outputStreamConsumer, Executor executor, int chunkSize) Inserter based on bytes written to aOutputStream
.- Parameters:
outputStreamConsumer
- invoked with anOutputStream
that writes to the output messageexecutor
- used to invoke theoutputStreamHandler
on a separate threadchunkSize
- minimum size of the buffer produced by the publisher- Returns:
- an inserter that writes what is written to the output stream
- Since:
- 6.1
- See Also:
-
fromValue(Object)