Package org.springframework.http.client
Class MultipartBodyBuilder
java.lang.Object
org.springframework.http.client.MultipartBodyBuilder
Prepare the body of a multipart request, resulting in a
 
MultiValueMap<String, HttpEntity>. Parts may be concrete values or
 via asynchronous types such as Reactor Mono, Flux, and
 others registered in the
 ReactiveAdapterRegistry.
 This builder is intended to POST multipart/form-data using the reactive
 WebClient.
 For multipart requests with the RestTemplate, simply create and
 populate a MultiValueMap<String, HttpEntity> as shown in the Javadoc for
 FormHttpMessageConverter
 and in the
 reference docs.
 
Below are examples of using this builder:
 // 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 (for example, 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("...")
     .bodyValue(multipartBody)
     .retrieve()
     .bodyToMono(Void.class)
 - Since:
- 5.0.2
- Author:
- Arjen Poutsma, Rossen Stoyanchev, Sam Brannen
- See Also:
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic interfaceBuilder that allows for further customization of part headers.
- 
Constructor SummaryConstructorsConstructorDescriptionCreates a new, empty instance of theMultipartBodyBuilder.
- 
Method SummaryModifier and TypeMethodDescription<T,P extends Publisher<T>> 
 MultipartBodyBuilder.PartBuilderAdd a part fromPublishercontent.<T,P extends Publisher<T>> 
 MultipartBodyBuilder.PartBuilderasyncPart(String name, P publisher, ParameterizedTypeReference<T> typeReference) Variant ofasyncPart(String, Publisher, Class)with aParameterizedTypeReferencefor the element type information.build()Return aMultiValueMapwith the configured parts.Add a part where the Object may be: String -- form fieldResource-- file part Object -- content to be encoded (for example, to JSON).Variant ofpart(String, Object)that also accepts a MediaType.
- 
Constructor Details- 
MultipartBodyBuilderpublic MultipartBodyBuilder()Creates a new, empty instance of theMultipartBodyBuilder.
 
- 
- 
Method Details- 
partAdd a part where the Object may be:- String -- form field
- Resource-- file part
- Object -- content to be encoded (for example, to JSON).
- HttpEntity-- part content and headers although generally it's easier to add headers through the returned builder
- Part-- a part from a server request
 - Parameters:
- name- the name of the part to add
- part- the part data
- Returns:
- builder that allows for further customization of part headers
 
- 
partpublic MultipartBodyBuilder.PartBuilder part(String name, Object part, @Nullable MediaType contentType) Variant ofpart(String, Object)that also accepts a MediaType.- Parameters:
- name- the name of the part to add
- part- the part data
- contentType- the media type to help with encoding the part
- Returns:
- builder that allows for further customization of part headers
 
- 
asyncPartpublic <T,P extends Publisher<T>> MultipartBodyBuilder.PartBuilder asyncPart(String name, P publisher, Class<T> elementClass) Add a part fromPublishercontent.- Parameters:
- name- the name of the part to add
- publisher- a Publisher of content for the part
- elementClass- the type of elements contained in the publisher
- Returns:
- builder that allows for further customization of part headers
 
- 
asyncPartpublic <T,P extends Publisher<T>> MultipartBodyBuilder.PartBuilder asyncPart(String name, P publisher, ParameterizedTypeReference<T> typeReference) Variant ofasyncPart(String, Publisher, Class)with aParameterizedTypeReferencefor the element type information.- Parameters:
- name- the name of the part to add
- publisher- the part contents
- typeReference- the type of elements contained in the publisher
- Returns:
- builder that allows for further customization of part headers
 
- 
buildReturn aMultiValueMapwith the configured parts.
 
-