@@ -22,8 +22,10 @@ By comparison to the <<integration.adoc#rest-resttemplate,RestTemplate>>, the
22
22
* supports both synchronous and asynchronous scenarios.
23
23
* supports streaming up or down from a server.
24
24
25
- For most concurrent scenarios, e.g. a sequence of possibly inter-dependent HTTP calls,
26
- or for making remote calls from the server-side, prefer using the `WebClient`.
25
+ The `RestTemplate` is not a good fit for use in non-blocking applications, and therefore
26
+ Spring WebFlux application should always use the `WebClient`. The `WebClient` should also
27
+ be preferred in Spring MVC, in most high concurrency scenarios, and for composing a
28
+ sequence of remote, inter-dependent calls.
27
29
28
30
29
31
@@ -201,9 +203,10 @@ You can also supply form data in-line via `BodyInserters`:
201
203
[[webflux-client-body-multipart]]
202
204
=== Multipart data
203
205
204
- To send multipart data, provide a `MultiValueMap<String, ?>` where values are either an
205
- Object representing the part body, or an `HttpEntity` representing the part body and
206
- headers. `MultipartBodyBuilder` can be used to build the parts:
206
+ To send multipart data, you need to provide a `MultiValueMap<String, ?>` whose values are
207
+ either Objects representing part content, or `HttpEntity` representing the content and
208
+ headers for a part. `MultipartBodyBuilder` provides a convenient API to prepare a
209
+ multipart request:
207
210
208
211
[source,java,intent=0]
209
212
[subs="verbatim,quotes"]
@@ -214,19 +217,36 @@ headers. `MultipartBodyBuilder` can be used to build the parts:
214
217
builder.part("jsonPart", new Person("Jason"));
215
218
216
219
MultiValueMap<String, HttpEntity<?>> parts = builder.build();
220
+ ----
221
+
222
+ In most cases you do not have to specify the `Content-Type` for each part. The content
223
+ type is determined automatically based on the `HttpMessageWriter` chosen to serialize it,
224
+ or in the case of a `Resource` based on the file extension. If necessary you can
225
+ explicitly provide the `MediaType` to use for each part through one fo the overloaded
226
+ builder `part` methods.
227
+
228
+ Once a `MultiValueMap` is prepared, the easiest way to pass it to the the `WebClient` is
229
+ through the `syncBody` method:
230
+
231
+ [source,java,intent=0]
232
+ [subs="verbatim,quotes"]
233
+ ----
234
+ MultipartBodyBuilder builder = ...;
217
235
218
236
Mono<Void> result = client.post()
219
237
.uri("/path", id)
220
- .syncBody(parts )
238
+ .syncBody(**builder.build()** )
221
239
.retrieve()
222
240
.bodyToMono(Void.class);
223
241
----
224
242
225
- Note that the content type for each part is automatically set based on the extension of the
226
- file being written or the type of Object. If you prefer you can also be more explicit and
227
- specify the content type for each part.
243
+ If the `MultiValueMap` contains at least one non-String value, which could also be
244
+ represent regular form data (i.e. "application/x-www-form-urlencoded"), you don't have to
245
+ set the `Content-Type` to "multipart/form-data". This is always the case when using
246
+ `MultipartBodyBuilder` which ensures an `HttpEntity` wrapper.
228
247
229
- You can also supply multipart data in-line via `BodyInserters`:
248
+ As an alternative to `MultipartBodyBuilder`, you can also provide multipart content,
249
+ inline-style, through the built-in `BodyInserters`. For example:
230
250
231
251
[source,java,intent=0]
232
252
[subs="verbatim,quotes"]
@@ -242,6 +262,7 @@ You can also supply multipart data in-line via `BodyInserters`:
242
262
243
263
244
264
265
+
245
266
[[webflux-client-builder]]
246
267
== Builder options
247
268
0 commit comments