Top Description Inners Fields Constructors Methods
org.springframework.http.client

public final Class MultipartBodyBuilder

extends Object
Class Inheritance
Imports
java.util.Arrays, .List, .Map, java.util.function.Consumer, org.reactivestreams.Publisher, org.springframework.core.ParameterizedTypeReference, .ResolvableType, .ResolvableTypeProvider, org.springframework.core.io.buffer.DataBuffer, org.springframework.http.HttpEntity, .HttpHeaders, .MediaType, org.springframework.http.codec.multipart.Part, org.springframework.lang.NonNull, .Nullable, org.springframework.util.Assert, .LinkedMultiValueMap, .MultiValueMap

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 (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("...")
    .bodyValue(multipartBody)
    .retrieve()
    .bodyToMono(Void.class)
Authors
Arjen Poutsma, Rossen Stoyanchev, Sam Brannen
Since
5.0.2
See Also
RFC 7578

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static interface
MultipartBodyBuilder.PartBuilder

Builder that allows for further customization of part headers.

pack-priv static class
MultipartBodyBuilder.PublisherEntity<
the type contained in the publisher
T
,
the publisher
P extends Publisher<T>
>

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

Field Summary

Modifier and TypeField and Description
private final LinkedMultiValueMap<String, MultipartBodyBuilder.DefaultPartBuilder>

Constructor Summary

AccessConstructor and Description
public
MultipartBodyBuilder()

Creates a new, empty instance of the MultipartBodyBuilder.

Method Summary

Modifier and TypeMethod and Description
public <T, P extends Publisher<T>> MultipartBodyBuilder.PartBuilder

Returns:

builder that allows for further customization of part headers
asyncPart
(String
the name of the part to add
name
,
P
a Publisher of content for the part
publisher
,
Class<T>
the type of elements contained in the publisher
elementClass
)

Add a part from Publisher content.

public <T, P extends Publisher<T>> MultipartBodyBuilder.PartBuilder

Returns:

builder that allows for further customization of part headers
asyncPart
(String
the name of the part to add
name
,
P
the part contents
publisher
,
ParameterizedTypeReference<T>
the type of elements contained in the publisher
typeReference
)

Variant of asyncPart(String, Publisher, Class) with a ParameterizedTypeReference for the element type information.

public MultiValueMap<String, HttpEntity<?>>
build()

Return a MultiValueMap with the configured parts.

public MultipartBodyBuilder.PartBuilder

Returns:

builder that allows for further customization of part headers
part
(String
the name of the part to add
name
,
Object
the part data
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 builder
  • Part -- a part from a server request
public MultipartBodyBuilder.PartBuilder

Returns:

builder that allows for further customization of part headers
part
(String
the name of the part to add
name
,
Object
the part data
part
,
MediaType
the media type to help with encoding the part
contentType
)

Variant of part(String, Object) that also accepts a MediaType.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

partsback to summary
private final LinkedMultiValueMap<String, MultipartBodyBuilder.DefaultPartBuilder> parts

Constructor Detail

MultipartBodyBuilderback to summary
public MultipartBodyBuilder()

Creates a new, empty instance of the MultipartBodyBuilder.

Method Detail

asyncPartback to summary
public <T, P extends Publisher<T>> MultipartBodyBuilder.PartBuilder asyncPart(String name, P publisher, Class<T> elementClass)

Add a part from Publisher content.

Parameters
name:String

the name of the part to add

publisher:P

a Publisher of content for the part

elementClass:Class<T>

the type of elements contained in the publisher

Returns:MultipartBodyBuilder.PartBuilder

builder that allows for further customization of part headers

asyncPartback to summary
public <T, P extends Publisher<T>> MultipartBodyBuilder.PartBuilder asyncPart(String name, P publisher, ParameterizedTypeReference<T> typeReference)

Variant of asyncPart(String, Publisher, Class) with a ParameterizedTypeReference for the element type information.

Parameters
name:String

the name of the part to add

publisher:P

the part contents

typeReference:ParameterizedTypeReference<T>

the type of elements contained in the publisher

Returns:MultipartBodyBuilder.PartBuilder

builder that allows for further customization of part headers

buildback to summary
public MultiValueMap<String, HttpEntity<?>> build()

Return a MultiValueMap with the configured parts.

partback to summary
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 builder
  • Part -- a part from a server request
Parameters
name:String

the name of the part to add

part:Object

the part data

Returns:MultipartBodyBuilder.PartBuilder

builder that allows for further customization of part headers

partback to summary
public MultipartBodyBuilder.PartBuilder part(String name, Object part, MediaType contentType)

Variant of part(String, Object) that also accepts a MediaType.

Parameters
name:String

the name of the part to add

part:Object

the part data

contentType:MediaType

the media type to help with encoding the part

Returns:MultipartBodyBuilder.PartBuilder

builder that allows for further customization of part headers

org.springframework.http.client back to summary

private Class MultipartBodyBuilder.DefaultPartBuilder

extends Object
implements PartBuilder
Class Inheritance
  • java.lang.Object
  • org.springframework.http.client.MultipartBodyBuilder.DefaultPartBuilder
All Implemented Interfaces
org.springframework.http.client.MultipartBodyBuilder.PartBuilder
Known Direct Subclasses
org.springframework.http.client.MultipartBodyBuilder.PublisherPartBuilder

Field Summary

Modifier and TypeField and Description
protected final Object
protected HttpHeaders
private final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public HttpEntity<?>
public MultipartBodyBuilder.PartBuilder
public MultipartBodyBuilder.PartBuilder
filename(String
the filename to set on the Content-Disposition
filename
)

Implements org.springframework.http.client.MultipartBodyBuilder.PartBuilder.filename.

Set the filename parameter for a file part.
public MultipartBodyBuilder.PartBuilder
header(String
the part header name
headerName
,
String...
the part header value(s)
headerValues
)

Implements org.springframework.http.client.MultipartBodyBuilder.PartBuilder.header.

Add part header values.
public MultipartBodyBuilder.PartBuilder
headers(Consumer<HttpHeaders>
consumer to manipulate the part headers with
headersConsumer
)

Implements org.springframework.http.client.MultipartBodyBuilder.PartBuilder.headers.

Manipulate the part headers through the given consumer.
private HttpHeaders
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bodyback to summary
protected final Object body
Annotations
@Nullable
headersback to summary
protected HttpHeaders headers
Annotations
@Nullable
nameback to summary
private final String name

Constructor Detail

DefaultPartBuilderback to summary
public DefaultPartBuilder(String name, HttpHeaders headers, Object body)

Method Detail

buildback to summary
public HttpEntity<?> build()
contentTypeback to summary
public MultipartBodyBuilder.PartBuilder contentType(MediaType contentType)

Implements org.springframework.http.client.MultipartBodyBuilder.PartBuilder.contentType.

Doc from org.springframework.http.client.MultipartBodyBuilder.PartBuilder.contentType.

Set the media type of the part.

Parameters
contentType:MediaType

the content type

Annotations
@Override
filenameback to summary
public MultipartBodyBuilder.PartBuilder filename(String filename)

Implements org.springframework.http.client.MultipartBodyBuilder.PartBuilder.filename.

Doc from org.springframework.http.client.MultipartBodyBuilder.PartBuilder.filename.

Set the filename parameter for a file part. This should not be necessary with Resource based parts that expose a filename but may be useful for Publisher parts.

Parameters
filename:String

the filename to set on the Content-Disposition

Annotations
@Override
headerback to summary
public MultipartBodyBuilder.PartBuilder header(String headerName, String... headerValues)

Implements org.springframework.http.client.MultipartBodyBuilder.PartBuilder.header.

Doc from org.springframework.http.client.MultipartBodyBuilder.PartBuilder.header.

Add part header values.

Parameters
headerName:String

the part header name

headerValues:String[]

the part header value(s)

Returns:MultipartBodyBuilder.PartBuilder

this builder

Annotations
@Override
headersback to summary
public MultipartBodyBuilder.PartBuilder headers(Consumer<HttpHeaders> headersConsumer)

Implements org.springframework.http.client.MultipartBodyBuilder.PartBuilder.headers.

Doc from org.springframework.http.client.MultipartBodyBuilder.PartBuilder.headers.

Manipulate the part headers through the given consumer.

Parameters
headersConsumer:Consumer<HttpHeaders>

consumer to manipulate the part headers with

Returns:MultipartBodyBuilder.PartBuilder

this builder

Annotations
@Override
initHeadersIfNecessaryback to summary
private HttpHeaders initHeadersIfNecessary()
org.springframework.http.client back to summary

public Interface MultipartBodyBuilder.PartBuilder

Known Direct Implementers
org.springframework.http.client.MultipartBodyBuilder.DefaultPartBuilder

Builder that allows for further customization of part headers.

Method Summary

Modifier and TypeMethod and Description
public MultipartBodyBuilder.PartBuilder
contentType(MediaType
the content type
contentType
)

Set the media type of the part.

public MultipartBodyBuilder.PartBuilder
filename(String
the filename to set on the Content-Disposition
filename
)

Set the filename parameter for a file part.

public MultipartBodyBuilder.PartBuilder

Returns:

this builder
header
(String
the part header name
headerName
,
String...
the part header value(s)
headerValues
)

Add part header values.

public MultipartBodyBuilder.PartBuilder

Returns:

this builder
headers
(Consumer<HttpHeaders>
consumer to manipulate the part headers with
headersConsumer
)

Manipulate the part headers through the given consumer.

Method Detail

contentTypeback to summary
public MultipartBodyBuilder.PartBuilder contentType(MediaType contentType)

Set the media type of the part.

Parameters
contentType:MediaType

the content type

Since
5.2
See Also
HttpHeaders#setContentType(MediaType)
filenameback to summary
public MultipartBodyBuilder.PartBuilder filename(String filename)

Set the filename parameter for a file part. This should not be necessary with Resource based parts that expose a filename but may be useful for Publisher parts.

Parameters
filename:String

the filename to set on the Content-Disposition

Since
5.2
headerback to summary
public MultipartBodyBuilder.PartBuilder header(String headerName, String... headerValues)

Add part header values.

Parameters
headerName:String

the part header name

headerValues:String[]

the part header value(s)

Returns:MultipartBodyBuilder.PartBuilder

this builder

See Also
HttpHeaders#addAll(String, List)
headersback to summary
public MultipartBodyBuilder.PartBuilder headers(Consumer<HttpHeaders> headersConsumer)

Manipulate the part headers through the given consumer.

Parameters
headersConsumer:Consumer<HttpHeaders>

consumer to manipulate the part headers with

Returns:MultipartBodyBuilder.PartBuilder

this builder

org.springframework.http.client back to summary

pack-priv final Class MultipartBodyBuilder.PublisherEntity<T, P extends org.reactivestreams.Publisher<T>>

extends HttpEntity<P extends Publisher<T>>
implements ResolvableTypeProvider
Class Inheritance
All Implemented Interfaces
org.springframework.core.ResolvableTypeProvider
Type Parameters
<T>
the type contained in the publisher
<P>
the publisher

Specialization of HttpEntity for use with a Publisher-based body, for which we also need to keep track of the element type.

Field Summary

Modifier and TypeField and Description
private final ResolvableType
Inherited from org.springframework.http.HttpEntity:
EMPTY

Constructor Summary

AccessConstructor and Description
pack-priv
PublisherEntity(MultiValueMap<String, String> headers, P publisher, ResolvableType resolvableType)

Method Summary

Modifier and TypeMethod and Description
public ResolvableType
getResolvableType()

Implements org.springframework.core.ResolvableTypeProvider.getResolvableType.

Return the element type for the Publisher body.
Inherited from org.springframework.http.HttpEntity:
equalsgetBodygetHeadershasBodyhashCodetoString

Field Detail

resolvableTypeback to summary
private final ResolvableType resolvableType

Constructor Detail

PublisherEntityback to summary
pack-priv PublisherEntity(MultiValueMap<String, String> headers, P publisher, ResolvableType resolvableType)

Method Detail

getResolvableTypeback to summary
public ResolvableType getResolvableType()

Implements org.springframework.core.ResolvableTypeProvider.getResolvableType.

Return the element type for the Publisher body.

Annotations
@Override
@NonNull
org.springframework.http.client back to summary

private Class MultipartBodyBuilder.PublisherPartBuilder<S, P extends org.reactivestreams.Publisher<S>>

extends DefaultPartBuilder
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final ResolvableType
Inherited from org.springframework.http.client.MultipartBodyBuilder.DefaultPartBuilder:
bodyheaders

Constructor Summary

AccessConstructor and Description
public
PublisherPartBuilder(String name, HttpHeaders headers, P body, Class<S> elementClass)

public
public

Method Summary

Modifier and TypeMethod and Description
public HttpEntity<?>
Inherited from org.springframework.http.client.MultipartBodyBuilder.DefaultPartBuilder:
contentTypefilenameheaderheaders

Field Detail

resolvableTypeback to summary
private final ResolvableType resolvableType

Constructor Detail

PublisherPartBuilderback to summary
public PublisherPartBuilder(String name, HttpHeaders headers, P body, Class<S> elementClass)
PublisherPartBuilderback to summary
public PublisherPartBuilder(String name, HttpHeaders headers, P body, ParameterizedTypeReference<S> typeRef)
PublisherPartBuilderback to summary
public PublisherPartBuilder(String name, MultipartBodyBuilder.PublisherEntity<S, P> other)

Method Detail

buildback to summary
public HttpEntity<?> build()

Overrides org.springframework.http.client.MultipartBodyBuilder.DefaultPartBuilder.build.

Annotations
@Override
@SuppressWarnings:unchecked