請求體

請求體可以從由 ReactiveAdapterRegistry 處理的任何非同步型別(例如 Mono 或 Kotlin Coroutines 的 Deferred)進行編碼,示例如下

  • Java

  • Kotlin

Mono<Person> personMono = ... ;

Mono<Void> result = client.post()
		.uri("/persons/{id}", id)
		.contentType(MediaType.APPLICATION_JSON)
		.body(personMono, Person.class)
		.retrieve()
		.bodyToMono(Void.class);
val personDeferred: Deferred<Person> = ...

client.post()
		.uri("/persons/{id}", id)
		.contentType(MediaType.APPLICATION_JSON)
		.body<Person>(personDeferred)
		.retrieve()
		.awaitBody<Unit>()

您也可以對物件流進行編碼,示例如下

  • Java

  • Kotlin

Flux<Person> personFlux = ... ;

Mono<Void> result = client.post()
		.uri("/persons/{id}", id)
		.contentType(MediaType.APPLICATION_STREAM_JSON)
		.body(personFlux, Person.class)
		.retrieve()
		.bodyToMono(Void.class);
val people: Flow<Person> = ...

client.post()
		.uri("/persons/{id}", id)
		.contentType(MediaType.APPLICATION_JSON)
		.body(people)
		.retrieve()
		.awaitBody<Unit>()

另外,如果您有實際值,可以使用 bodyValue 快捷方法,示例如下

  • Java

  • Kotlin

Person person = ... ;

Mono<Void> result = client.post()
		.uri("/persons/{id}", id)
		.contentType(MediaType.APPLICATION_JSON)
		.bodyValue(person)
		.retrieve()
		.bodyToMono(Void.class);
val person: Person = ...

client.post()
		.uri("/persons/{id}", id)
		.contentType(MediaType.APPLICATION_JSON)
		.bodyValue(person)
		.retrieve()
		.awaitBody<Unit>()

表單資料

要傳送表單資料,您可以提供 MultiValueMap<String, String> 作為請求體。請注意,內容型別將由 FormHttpMessageWriter 自動設定為 application/x-www-form-urlencoded。以下示例展示瞭如何使用 MultiValueMap<String, String>

  • Java

  • Kotlin

MultiValueMap<String, String> formData = ... ;

Mono<Void> result = client.post()
		.uri("/path", id)
		.bodyValue(formData)
		.retrieve()
		.bodyToMono(Void.class);
val formData: MultiValueMap<String, String> = ...

client.post()
		.uri("/path", id)
		.bodyValue(formData)
		.retrieve()
		.awaitBody<Unit>()

您也可以透過使用 BodyInserters 內聯提供表單資料,示例如下

  • Java

  • Kotlin

import static org.springframework.web.reactive.function.BodyInserters.*;

Mono<Void> result = client.post()
		.uri("/path", id)
		.body(fromFormData("k1", "v1").with("k2", "v2"))
		.retrieve()
		.bodyToMono(Void.class);
import org.springframework.web.reactive.function.BodyInserters.*

client.post()
		.uri("/path", id)
		.body(fromFormData("k1", "v1").with("k2", "v2"))
		.retrieve()
		.awaitBody<Unit>()

Multipart 資料

要傳送 multipart 資料,您需要提供一個 MultiValueMap<String, ?>,其值可以是表示部分內容的 Object 例項,也可以是表示部分內容和頭的 HttpEntity 例項。MultipartBodyBuilder 提供了一個方便的 API 來準備 multipart 請求。以下示例展示瞭如何建立一個 MultiValueMap<String, ?>

  • Java

  • Kotlin

MultipartBodyBuilder builder = new MultipartBodyBuilder();
builder.part("fieldPart", "fieldValue");
builder.part("filePart1", new FileSystemResource("...logo.png"));
builder.part("jsonPart", new Person("Jason"));
builder.part("myPart", part); // Part from a server request

MultiValueMap<String, HttpEntity<?>> parts = builder.build();
val builder = MultipartBodyBuilder().apply {
	part("fieldPart", "fieldValue")
	part("filePart1", FileSystemResource("...logo.png"))
	part("jsonPart", Person("Jason"))
	part("myPart", part) // Part from a server request
}

val parts = builder.build()

在大多數情況下,您無需為每個部分指定 Content-Type。內容型別會根據用於序列化它的 HttpMessageWriter 自動確定,或者在 Resource 的情況下,根據副檔名自動確定。如有必要,您可以透過過載的 builder part 方法之一明確提供每個部分使用的 MediaType

準備好 MultiValueMap 後,將其傳遞給 WebClient 的最簡單方法是透過 body 方法,示例如下

  • Java

  • Kotlin

MultipartBodyBuilder builder = ...;

Mono<Void> result = client.post()
		.uri("/path", id)
		.body(builder.build())
		.retrieve()
		.bodyToMono(Void.class);
val builder: MultipartBodyBuilder = ...

client.post()
		.uri("/path", id)
		.body(builder.build())
		.retrieve()
		.awaitBody<Unit>()

如果 MultiValueMap 包含至少一個非 String 值(這也可能代表常規表單資料,即 application/x-www-form-urlencoded),則無需將 Content-Type 設定為 multipart/form-data。使用 MultipartBodyBuilder 時總是如此,它確保了 HttpEntity 包裝器。

作為 MultipartBodyBuilder 的替代方案,您也可以透過內建的 BodyInserters 以內聯方式提供 multipart 內容,示例如下

  • Java

  • Kotlin

import static org.springframework.web.reactive.function.BodyInserters.*;

Mono<Void> result = client.post()
		.uri("/path", id)
		.body(fromMultipartData("fieldPart", "value").with("filePart", resource))
		.retrieve()
		.bodyToMono(Void.class);
import org.springframework.web.reactive.function.BodyInserters.*

client.post()
		.uri("/path", id)
		.body(fromMultipartData("fieldPart", "value").with("filePart", resource))
		.retrieve()
		.awaitBody<Unit>()

PartEvent

要按順序流式傳輸 multipart 資料,您可以透過 PartEvent 物件提供 multipart 內容。

  • 可以透過 FormPartEvent::create 建立表單欄位。

  • 可以透過 FilePartEvent::create 建立檔案上傳。

您可以透過 Flux::concat 連線方法返回的流,併為 WebClient 建立一個請求。

例如,此示例將 POST 一個包含一個表單欄位和一個檔案的 multipart 表單。

  • Java

  • Kotlin

Resource resource = ...
Mono<String> result = webClient
	.post()
	.uri("https://example.com")
	.body(Flux.concat(
			FormPartEvent.create("field", "field value"),
			FilePartEvent.create("file", resource)
	), PartEvent.class)
	.retrieve()
	.bodyToMono(String.class);
var resource: Resource = ...
var result: Mono<String> = webClient
	.post()
	.uri("https://example.com")
	.body(
		Flux.concat(
			FormPartEvent.create("field", "field value"),
			FilePartEvent.create("file", resource)
		)
	)
	.retrieve()
	.bodyToMono()

在伺服器端,透過 @RequestBodyServerRequest::bodyToFlux(PartEvent.class) 接收的 PartEvent 物件可以透過 WebClient 轉發到另一個服務。