Package org.springframework.http.client
Class MultipartBodyBuilder
- java.lang.Object
- org.springframework.http.client.MultipartBodyBuilder
public final class MultipartBodyBuilder extends Object
Prepare the body of a multipart request, resulting in aMultiValueMap<String, HttpEntity>
. Parts may be concrete values or via asynchronous types such as ReactorMono
,Flux
, and others registered in theReactiveAdapterRegistry
.This builder is intended for use with the reactive
WebClient
. For multipart requests with theRestTemplate
, simply create and populate aMultiValueMap<String, HttpEntity>
as shown in the Javadoc forFormHttpMessageConverter
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 (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("...") .body(multipartBody) .retrieve() .bodyToMono(Void.class)
- Since:
- 5.0.2
- Author:
- Arjen Poutsma, Rossen Stoyanchev
- See Also:
- RFC 7578
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
MultipartBodyBuilder.PartBuilder
Builder that allows for further customization of part headers.
Constructor Summary
Constructors Constructor Description MultipartBodyBuilder()
Creates a new, empty instance of theMultipartBodyBuilder
.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T,P extends org.reactivestreams.Publisher<T>>
MultipartBodyBuilder.PartBuilderasyncPart(String name, P publisher, Class<T> elementClass)
Add a part fromPublisher
content.<T,P extends org.reactivestreams.Publisher<T>>
MultipartBodyBuilder.PartBuilderasyncPart(String name, P publisher, ParameterizedTypeReference<T> typeReference)
Variant ofasyncPart(String, Publisher, Class)
with aParameterizedTypeReference
for the element type information.MultiValueMap<String,HttpEntity<?>>
build()
Return aMultiValueMap
with the configured parts.MultipartBodyBuilder.PartBuilder
part(String name, Object part)
Add a part where the Object may be: String -- form fieldResource
-- file part Object -- content to be encoded (e.g.MultipartBodyBuilder.PartBuilder
part(String name, Object part, MediaType contentType)
Variant ofpart(String, Object)
that also accepts a MediaType.
Constructor Detail
MultipartBodyBuilder
public MultipartBodyBuilder()
Creates a new, empty instance of theMultipartBodyBuilder
.
Method Detail
part
public MultipartBodyBuilder.PartBuilder part(String name, Object part)
Add a part where the Object may be:- String -- form field
Resource
-- file part- Object -- content to be encoded (e.g. to JSON)
HttpEntity
-- part content and headers although generally it's easier to add headers through the returned builderPart
-- a part from a server request
- Parameters:
name
- the name of the part to addpart
- the part data- Returns:
- builder that allows for further customization of part headers
part
public 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 addpart
- the part datacontentType
- the media type to help with encoding the part- Returns:
- builder that allows for further customization of part headers
asyncPart
public <T,P extends org.reactivestreams.Publisher<T>> MultipartBodyBuilder.PartBuilder asyncPart(String name, P publisher, Class<T> elementClass)
Add a part fromPublisher
content.- Parameters:
name
- the name of the part to addpublisher
- a Publisher of content for the partelementClass
- the type of elements contained in the publisher- Returns:
- builder that allows for further customization of part headers
asyncPart
public <T,P extends org.reactivestreams.Publisher<T>> MultipartBodyBuilder.PartBuilder asyncPart(String name, P publisher, ParameterizedTypeReference<T> typeReference)
Variant ofasyncPart(String, Publisher, Class)
with aParameterizedTypeReference
for the element type information.- Parameters:
name
- the name of the part to addpublisher
- the part contentstypeReference
- the type of elements contained in the publisher- Returns:
- builder that allows for further customization of part headers
build
public MultiValueMap<String,HttpEntity<?>> build()
Return aMultiValueMap
with the configured parts.