public abstract class BodyInserters
extends java.lang.Object
BodyInserter
that write various bodies, such a reactive streams,
server-sent events, resources, etc.Modifier and Type | Class and Description |
---|---|
private static class |
BodyInserters.DefaultFormInserter |
private static class |
BodyInserters.DefaultMultipartInserter |
static interface |
BodyInserters.FormInserter<T>
Sub-interface of
BodyInserter that allows for additional (multipart) form data to be
added. |
static interface |
BodyInserters.MultipartInserter
Extension of
BodyInserters.FormInserter that has methods for adding asynchronous part data. |
Modifier and Type | Field and Description |
---|---|
private static BodyInserter<java.lang.Void,ReactiveHttpOutputMessage> |
EMPTY |
private static ResolvableType |
FORM_TYPE |
private static ResolvableType |
MULTIPART_VALUE_TYPE |
private static ResolvableType |
RESOURCE_TYPE |
private static ResolvableType |
SERVER_SIDE_EVENT_TYPE |
Constructor and Description |
---|
BodyInserters() |
Modifier and Type | Method and Description |
---|---|
private static <T,P extends <any>,M extends ReactiveHttpOutputMessage> |
bodyInserterFor(P body,
ResolvableType bodyType) |
private static <T> HttpMessageWriter<T> |
cast(HttpMessageWriter<?> messageWriter) |
static <T> BodyInserter<T,ReactiveHttpOutputMessage> |
empty()
Return an empty
BodyInserter that writes nothing. |
private static <T> HttpMessageWriter<T> |
findMessageWriter(BodyInserter.Context context,
ResolvableType type,
MediaType mediaType) |
static <T extends <any>> |
fromDataBuffers(T publisher)
Return a
BodyInserter that writes the given
Publisher<DataBuffer> to the body. |
static BodyInserters.FormInserter<java.lang.String> |
fromFormData(MultiValueMap<java.lang.String,java.lang.String> formData)
Return a
BodyInserters.FormInserter that writes the given MultiValueMap
as URL-encoded form data. |
static BodyInserters.FormInserter<java.lang.String> |
fromFormData(java.lang.String key,
java.lang.String value)
Return a
BodyInserters.FormInserter that writes the given key-value pair as
URL-encoded form data. |
static <T,P extends <any>> |
fromMultipartAsyncData(java.lang.String key,
P publisher,
java.lang.Class<T> elementClass)
A variant of
fromMultipartData(MultiValueMap) for adding asynchronous data as a
part in-line vs building a MultiValueMap and passing it in. |
static <T,P extends <any>> |
fromMultipartAsyncData(java.lang.String key,
P publisher,
ParameterizedTypeReference<T> typeReference)
A variant of
fromMultipartData(MultiValueMap) for adding asynchronous data as a
part in-line vs building a MultiValueMap and passing it in. |
static BodyInserters.MultipartInserter |
fromMultipartData(MultiValueMap<java.lang.String,?> multipartData)
Return a
FormInserter that writes the given MultiValueMap
as multipart data. |
static BodyInserters.MultipartInserter |
fromMultipartData(java.lang.String key,
java.lang.Object value)
A variant of
fromMultipartData(MultiValueMap) for adding
parts as name-value pairs in-line vs building a MultiValueMap
and passing it in. |
static <T> BodyInserter<T,ReactiveHttpOutputMessage> |
fromObject(T body)
Return a
BodyInserter that writes the given single object. |
static <T,P extends <any>> |
fromPublisher(P publisher,
java.lang.Class<T> elementClass)
Return a
BodyInserter that writes the given Publisher . |
static <T,P extends <any>> |
fromPublisher(P publisher,
ParameterizedTypeReference<T> typeReference)
Return a
BodyInserter that writes the given Publisher . |
static <T extends Resource> |
fromResource(T resource)
Return a
BodyInserter that writes the given Resource . |
static <T,S extends <any>> |
fromServerSentEvents(S eventsPublisher)
Return a
BodyInserter that writes the given ServerSentEvent publisher. |
private static HttpMessageWriter<Resource> |
resourceHttpMessageWriter(BodyInserter.Context context) |
private static final ResolvableType RESOURCE_TYPE
private static final ResolvableType SERVER_SIDE_EVENT_TYPE
private static final ResolvableType FORM_TYPE
private static final ResolvableType MULTIPART_VALUE_TYPE
private static final BodyInserter<java.lang.Void,ReactiveHttpOutputMessage> EMPTY
public static <T> BodyInserter<T,ReactiveHttpOutputMessage> empty()
BodyInserter
that writes nothing.BodyInserter
public static <T> BodyInserter<T,ReactiveHttpOutputMessage> fromObject(T body)
BodyInserter
that writes the given single object.
Note also that
WebClient
and
ServerResponse
each offer a syncBody(Object)
shortcut for providing an Object
as the body.
body
- the body of the responseBodyInserter
that writes a single objectpublic static <T,P extends <any>> BodyInserter<P,ReactiveHttpOutputMessage> fromPublisher(P publisher, java.lang.Class<T> elementClass)
BodyInserter
that writes the given Publisher
.
Note also that
WebClient
and
ServerResponse
each offer body
shortcut methods for providing a Publisher as the body.
T
- the type of the elements contained in the publisherP
- the type of the Publisher
publisher
- the publisher to stream to the response bodyelementClass
- the class of elements contained in the publisherBodyInserter
that writes a Publisher
public static <T,P extends <any>> BodyInserter<P,ReactiveHttpOutputMessage> fromPublisher(P publisher, ParameterizedTypeReference<T> typeReference)
BodyInserter
that writes the given Publisher
.
Note also that
WebClient
and
ServerResponse
each offer body
shortcut methods for providing a Publisher as the body.
T
- the type of the elements contained in the publisherP
- the type of the Publisher
publisher
- the publisher to stream to the response bodytypeReference
- the type of elements contained in the publisherBodyInserter
that writes a Publisher
public static <T extends Resource> BodyInserter<T,ReactiveHttpOutputMessage> fromResource(T resource)
BodyInserter
that writes the given Resource
.
If the resource can be resolved to a file, it will be copied using zero-copy.
T
- the type of the Resource
resource
- the resource to write to the output messageBodyInserter
that writes a Publisher
private static HttpMessageWriter<Resource> resourceHttpMessageWriter(BodyInserter.Context context)
public static <T,S extends <any>> BodyInserter<S,ServerHttpResponse> fromServerSentEvents(S eventsPublisher)
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.
T
- the type of the elements contained in the ServerSentEvent
eventsPublisher
- the ServerSentEvent
publisher to write to the response bodyBodyInserter
that writes a ServerSentEvent
publisherpublic static BodyInserters.FormInserter<java.lang.String> fromFormData(MultiValueMap<java.lang.String,java.lang.String> formData)
BodyInserters.FormInserter
that writes the given MultiValueMap
as URL-encoded form data. The returned inserter allows for additional
entries to be added via BodyInserters.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.
formData
- the form data to write to the output messageFormInserter
that writes form datapublic static BodyInserters.FormInserter<java.lang.String> fromFormData(java.lang.String key, java.lang.String value)
BodyInserters.FormInserter
that writes the given key-value pair as
URL-encoded form data. The returned inserter allows for additional
entries to be added via BodyInserters.FormInserter.with(String, Object)
.key
- the key to add to the formvalue
- the value to add to the formFormInserter
that writes form datapublic static BodyInserters.MultipartInserter fromMultipartData(MultiValueMap<java.lang.String,?> multipartData)
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
HttpEntity
representing a part
with body and headers. The MultiValueMap
can be built conveniently
using MultipartBodyBuilder
. Also the returned inserter allows for additional
entries to be added via BodyInserters.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.
multipartData
- the form data to write to the output messageBodyInserter
that writes multipart dataMultipartBodyBuilder
public static BodyInserters.MultipartInserter fromMultipartData(java.lang.String key, java.lang.Object value)
fromMultipartData(MultiValueMap)
for adding
parts as name-value pairs in-line vs building a MultiValueMap
and passing it in.key
- the part namevalue
- the part value, an Object or HttpEntity
FormInserter
that can writes the provided multipart
data and also allows adding more partspublic static <T,P extends <any>> BodyInserters.MultipartInserter fromMultipartAsyncData(java.lang.String key, P publisher, java.lang.Class<T> elementClass)
fromMultipartData(MultiValueMap)
for adding asynchronous data as a
part in-line vs building a MultiValueMap
and passing it in.key
- the part namepublisher
- the publisher that forms the part valueelementClass
- the class contained in the publisher
FormInserter
that can writes the provided multipart
data and also allows adding more partspublic static <T,P extends <any>> BodyInserters.MultipartInserter fromMultipartAsyncData(java.lang.String key, P publisher, ParameterizedTypeReference<T> typeReference)
fromMultipartData(MultiValueMap)
for adding asynchronous data as a
part in-line vs building a MultiValueMap
and passing it in.key
- the part namepublisher
- the publisher that forms the part valuetypeReference
- the type contained in the publisher
FormInserter
that can writes the provided multipart
data and also allows adding more partspublic static <T extends <any>> BodyInserter<T,ReactiveHttpOutputMessage> fromDataBuffers(T publisher)
BodyInserter
that writes the given
Publisher<DataBuffer>
to the body.T
- the type of the publisherpublisher
- the data buffer publisher to writeBodyInserter
that writes directly to the bodyReactiveHttpOutputMessage#writeWith(Publisher)
private static <T,P extends <any>,M extends ReactiveHttpOutputMessage> BodyInserter<T,M> bodyInserterFor(P body, ResolvableType bodyType)
private static <T> HttpMessageWriter<T> findMessageWriter(BodyInserter.Context context, ResolvableType type, MediaType mediaType)
private static <T> HttpMessageWriter<T> cast(HttpMessageWriter<?> messageWriter)