public final class MultipartBodyBuilder
extends java.lang.Object
MultiValueMap<String, HttpEntity>
, which can be provided to the
WebClient
through the syncBody
method.
Examples:
// Add form field MultipartBodyBuilder builder = new MultipartBodyBuilder(); builder.part("form field", "form value").header("foo", "bar"); // Add file part Resource image = new ClassPathResource("image.jpg"); builder.part("image", image).header("foo", "bar"); // Add content (e.g. JSON) Account account = ... builder.part("account", account).header("foo", "bar"); // Add content from Publisher Mono<Account> accountMono = ... builder.asyncPart("account", accountMono).header("foo", "bar"); // Build and use MultiValueMap<String, HttpEntity<?>> multipartBody = builder.build(); Mono<Void> result = webClient.post() .uri("...") .syncBody(multipartBody) .retrieve() .bodyToMono(Void.class)
Modifier and Type | Class and Description |
---|---|
private static class |
MultipartBodyBuilder.DefaultPartBuilder |
static interface |
MultipartBodyBuilder.PartBuilder
Builder that allows for further customization of part headers.
|
(package private) static class |
MultipartBodyBuilder.PublisherEntity<T,P extends <any>>
Specialization of
HttpEntity for use with a
Publisher -based body, for which we also need to keep track of
the element type. |
private static class |
MultipartBodyBuilder.PublisherPartBuilder<S,P extends <any>> |
Modifier and Type | Field and Description |
---|---|
private LinkedMultiValueMap<java.lang.String,MultipartBodyBuilder.DefaultPartBuilder> |
parts |
Constructor and Description |
---|
MultipartBodyBuilder()
Creates a new, empty instance of the
MultipartBodyBuilder . |
Modifier and Type | Method and Description |
---|---|
<T,P extends <any>> |
asyncPart(java.lang.String name,
P publisher,
java.lang.Class<T> elementClass)
Add a part from
Publisher content. |
<T,P extends <any>> |
asyncPart(java.lang.String name,
P publisher,
ParameterizedTypeReference<T> typeReference)
Variant of
#asyncPart(String, Publisher, Class) with a
ParameterizedTypeReference for the element type information. |
MultiValueMap<java.lang.String,HttpEntity<?>> |
build()
Return a
MultiValueMap with the configured parts. |
MultipartBodyBuilder.PartBuilder |
part(java.lang.String name,
java.lang.Object part)
Add a part where the Object may be:
String -- form field
Resource -- file part
Object -- content to be encoded (e.g. |
MultipartBodyBuilder.PartBuilder |
part(java.lang.String name,
java.lang.Object part,
MediaType contentType)
Variant of
part(String, Object) that also accepts a MediaType. |
private final LinkedMultiValueMap<java.lang.String,MultipartBodyBuilder.DefaultPartBuilder> parts
public MultipartBodyBuilder()
MultipartBodyBuilder
.public MultipartBodyBuilder.PartBuilder part(java.lang.String name, java.lang.Object part)
Resource
-- file part
name
- the name of the part to addpart
- the part datapublic MultipartBodyBuilder.PartBuilder part(java.lang.String name, java.lang.Object part, @Nullable MediaType contentType)
part(String, Object)
that also accepts a MediaType.name
- the name of the part to addpart
- the part datacontentType
- the media type to help with encoding the partpublic <T,P extends <any>> MultipartBodyBuilder.PartBuilder asyncPart(java.lang.String name, P publisher, java.lang.Class<T> elementClass)
Publisher
content.name
- the name of the part to addpublisher
- the part contentselementClass
- the type of elements contained in the publisherpublic <T,P extends <any>> MultipartBodyBuilder.PartBuilder asyncPart(java.lang.String name, P publisher, ParameterizedTypeReference<T> typeReference)
#asyncPart(String, Publisher, Class)
with a
ParameterizedTypeReference
for the element type information.name
- the name of the part to addpublisher
- the part contentstypeReference
- the type of elements contained in the publisherpublic MultiValueMap<java.lang.String,HttpEntity<?>> build()
MultiValueMap
with the configured parts.