public abstract class BodyInserters
extends java.lang.Object
BodyInserter
implementations.Modifier and Type | Class and Description |
---|---|
private static class |
BodyInserters.DefaultFormInserter |
private static class |
BodyInserters.DefaultMultipartInserter |
static interface |
BodyInserters.FormInserter<T>
Extension of
BodyInserter that allows for adding form data or
multipart form data. |
static interface |
BodyInserters.MultipartInserter
Extension of
BodyInserters.FormInserter that allows for adding asynchronous parts. |
Modifier and Type | Field and Description |
---|---|
private static BodyInserter<java.lang.Void,ReactiveHttpOutputMessage> |
EMPTY_INSERTER |
private static ResolvableType |
FORM_DATA_TYPE |
private static ResolvableType |
MULTIPART_DATA_TYPE |
private static ResolvableType |
RESOURCE_TYPE |
private static ResolvableType |
SSE_TYPE |
Constructor and Description |
---|
BodyInserters() |
Modifier and Type | Method and Description |
---|---|
private static <T> HttpMessageWriter<T> |
cast(HttpMessageWriter<?> messageWriter) |
static <T> BodyInserter<T,ReactiveHttpOutputMessage> |
empty()
Inserter that does not write.
|
private static <T> HttpMessageWriter<T> |
findWriter(BodyInserter.Context context,
ResolvableType elementType,
MediaType mediaType) |
static <T extends <any>> |
fromDataBuffers(T publisher)
Inserter to write 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 to write the given MultiValueMap
as URL-encoded form data. |
static BodyInserters.FormInserter<java.lang.String> |
fromFormData(java.lang.String name,
java.lang.String value)
Return a
BodyInserters.FormInserter to write the given key-value pair as
URL-encoded form data. |
static <T,P extends <any>> |
fromMultipartAsyncData(java.lang.String name,
P publisher,
java.lang.Class<T> elementClass)
Return a
BodyInserters.MultipartInserter to write the given asynchronous parts,
as multipart data. |
static <T,P extends <any>> |
fromMultipartAsyncData(java.lang.String name,
P publisher,
ParameterizedTypeReference<T> typeReference)
Variant of
#fromMultipartAsyncData(String, Publisher, Class) that
accepts a ParameterizedTypeReference for the element type, which
allows specifying generic type information. |
static BodyInserters.MultipartInserter |
fromMultipartData(MultiValueMap<java.lang.String,?> multipartData)
Return a
BodyInserters.MultipartInserter to write the given
MultiValueMap as multipart data. |
static BodyInserters.MultipartInserter |
fromMultipartData(java.lang.String name,
java.lang.Object value)
Return a
BodyInserters.MultipartInserter to write the given parts,
as multipart data. |
static <T> BodyInserter<T,ReactiveHttpOutputMessage> |
fromObject(T body)
Inserter to write the given object.
|
static <T,P extends <any>> |
fromPublisher(P publisher,
java.lang.Class<T> elementClass)
Inserter to write the given
Publisher . |
static <T,P extends <any>> |
fromPublisher(P publisher,
ParameterizedTypeReference<T> typeReference)
Inserter to write the given
Publisher . |
static <T extends Resource> |
fromResource(T resource)
Inserter to write the given
Resource . |
static <T,S extends <any>> |
fromServerSentEvents(S eventsPublisher)
Inserter to write the given
ServerSentEvent publisher. |
private static UnsupportedMediaTypeException |
unsupportedError(ResolvableType bodyType,
BodyInserter.Context context,
MediaType mediaType) |
private static <T> <any> |
write(<any> input,
ResolvableType type,
MediaType mediaType,
ReactiveHttpOutputMessage message,
BodyInserter.Context context,
HttpMessageWriter<T> writer) |
private static <P extends <any>,M extends ReactiveHttpOutputMessage> |
writeWithMessageWriters(M outputMessage,
BodyInserter.Context context,
P body,
ResolvableType bodyType) |
private static final ResolvableType RESOURCE_TYPE
private static final ResolvableType SSE_TYPE
private static final ResolvableType FORM_DATA_TYPE
private static final ResolvableType MULTIPART_DATA_TYPE
private static final BodyInserter<java.lang.Void,ReactiveHttpOutputMessage> EMPTY_INSERTER
public static <T> BodyInserter<T,ReactiveHttpOutputMessage> empty()
public static <T> BodyInserter<T,ReactiveHttpOutputMessage> fromObject(T body)
Alternatively, consider using the syncBody(Object)
shortcuts on
WebClient
and
ServerResponse
.
T
- the type of the bodybody
- the body to write to the responsepublic static <T,P extends <any>> BodyInserter<P,ReactiveHttpOutputMessage> fromPublisher(P publisher, java.lang.Class<T> elementClass)
Publisher
.
Alternatively, consider using the body
shortcuts on
WebClient
and
ServerResponse
.
T
- the type of the elements contained in the publisherP
- the Publisher
typepublisher
- the publisher to write withelementClass
- the type of elements in the publisherPublisher
public static <T,P extends <any>> BodyInserter<P,ReactiveHttpOutputMessage> fromPublisher(P publisher, ParameterizedTypeReference<T> typeReference)
Publisher
.
Alternatively, consider using the body
shortcuts on
WebClient
and
ServerResponse
.
T
- the type of the elements contained in the publisherP
- the Publisher
typepublisher
- the publisher to write withtypeReference
- the type of elements contained in the publisherPublisher
public static <T extends Resource> BodyInserter<T,ReactiveHttpOutputMessage> fromResource(T resource)
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 messagePublisher
public static <T,S extends <any>> BodyInserter<S,ServerHttpResponse> fromServerSentEvents(S eventsPublisher)
ServerSentEvent
publisher.
Alternatively, you can provide event data objects via
#fromPublisher(Publisher, Class)
, and set the "Content-Type" to
text/event-stream
.
T
- the type of the data elements in the ServerSentEvent
eventsPublisher
- the ServerSentEvent
publisher to write to the response bodyServerSentEvent
publisherpublic static BodyInserters.FormInserter<java.lang.String> fromFormData(MultiValueMap<java.lang.String,java.lang.String> formData)
BodyInserters.FormInserter
to write 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 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.
formData
- the form data to write to the output messagepublic static BodyInserters.FormInserter<java.lang.String> fromFormData(java.lang.String name, java.lang.String value)
BodyInserters.FormInserter
to write 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)
.name
- the key to add to the formvalue
- the value to add to the formpublic static BodyInserters.MultipartInserter fromMultipartData(MultiValueMap<java.lang.String,?> multipartData)
BodyInserters.MultipartInserter
to write the given
MultiValueMap
as multipart data. Values in the map can be an
Object or an HttpEntity
.
Note that you can also build the multipart data externally with
MultipartBodyBuilder
, and pass the resulting map directly to the
syncBody(Object)
shortcut method in WebClient
.
multipartData
- the form data to write to the output messageMultipartBodyBuilder
public static BodyInserters.MultipartInserter fromMultipartData(java.lang.String name, java.lang.Object value)
BodyInserters.MultipartInserter
to write the given parts,
as multipart data. Values in the map can be an Object or an
HttpEntity
.
Note that you can also build the multipart data externally with
MultipartBodyBuilder
, and pass the resulting map directly to the
syncBody(Object)
shortcut method in WebClient
.
name
- the part namevalue
- the part value, an Object or HttpEntity
public static <T,P extends <any>> BodyInserters.MultipartInserter fromMultipartAsyncData(java.lang.String name, P publisher, java.lang.Class<T> elementClass)
BodyInserters.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 the
syncBody(Object)
shortcut method in WebClient
.
name
- the part namepublisher
- the publisher that forms the part valueelementClass
- the class contained in the publisher
public static <T,P extends <any>> BodyInserters.MultipartInserter fromMultipartAsyncData(java.lang.String name, P publisher, ParameterizedTypeReference<T> typeReference)
#fromMultipartAsyncData(String, Publisher, Class)
that
accepts a ParameterizedTypeReference
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 the
syncBody(Object)
shortcut method in WebClient
.
name
- the part namepublisher
- the publisher that forms the part valuetypeReference
- the type contained in the publisher
public static <T extends <any>> BodyInserter<T,ReactiveHttpOutputMessage> fromDataBuffers(T publisher)
Publisher<DataBuffer>
to the body.T
- the type of the publisherpublisher
- the data buffer publisher to writeReactiveHttpOutputMessage#writeWith(Publisher)
private static <P extends <any>,M extends ReactiveHttpOutputMessage> <any> writeWithMessageWriters(M outputMessage, BodyInserter.Context context, P body, ResolvableType bodyType)
private static UnsupportedMediaTypeException unsupportedError(ResolvableType bodyType, BodyInserter.Context context, @Nullable MediaType mediaType)
private static <T> <any> write(<any> input, ResolvableType type, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, BodyInserter.Context context, HttpMessageWriter<T> writer)
private static <T> HttpMessageWriter<T> findWriter(BodyInserter.Context context, ResolvableType elementType, @Nullable MediaType mediaType)
private static <T> HttpMessageWriter<T> cast(HttpMessageWriter<?> messageWriter)