When writing multipart data, this converter uses other
HttpMessageConverters to write the respective
MIME parts. By default, basic converters are registered (for Strings
and Resources). These can be overridden through the
For example, the following snippet shows how to submit an HTML form:
RestTemplate template = new RestTemplate();
// AllEncompassingFormHttpMessageConverter is configured by default
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
form.add("field 1", "value 1");
form.add("field 2", "value 2");
form.add("field 2", "value 3");
The following snippet shows how to do a file upload:
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
parts.add("field 1", "value 1");
parts.add("file", new ClassPathResource("myFile.jpg"));
Some methods in this class were inspired by
Add a message body converter. Such a converter is used to convert objects
to MIME parts.
public void setCharset(@Nullable
Set the default character set to use for reading and writing form data when
the request or response Content-Type header does not explicitly specify it.
As of 4.3, this is also used as the default charset for the conversion
of text bodies in a multipart request.
As of 5.0 this is also used for part headers including
"Content-Disposition" (and its filename parameter) unless (the mutually
exclusive) setMultipartCharset(java.nio.charset.Charset) is also set, in which case part
headers are encoded as ASCII and filename is encoded with the
"encoded-word" syntax from RFC 2047.
By default this is set to "UTF-8".
public void setMultipartCharset(java.nio.charset.Charset charset)
Set the character set to use when writing multipart data to encode file
names. Encoding is based on the "encoded-word" syntax defined in RFC 2047
and relies on MimeUtility from "javax.mail".
As of 5.0 by default part headers, including Content-Disposition (and
its filename parameter) will be encoded based on the setting of
setCharset(Charset) or UTF-8 by default.
map - the object to write to the output message. The type of this object must have previously been
passed to the canWrite method of this interface, which must have returned true.
contentType - the content type to use when writing. May be null to indicate that the
default content type of the converter must be used. If not null, this media type must have
previously been passed to the canWrite method of this interface, which must have