请求体

请求体可以从 ReactiveAdapterRegistry 处理的任何异步类型进行编码,例如 Mono 或 Kotlin 协程的 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 中继到另一个服务。