请求正文可以从 处理的任何异步类型进行编码,由 处理 like 或 Kotlin 协程,如以下示例所示:ReactiveAdapterRegistryMonoDeferredSpring中文文档

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>()

还可以对对象流进行编码,如以下示例所示:Spring中文文档

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>()

或者,如果你有实际值,你可以使用快捷方式方法, 如以下示例所示:bodyValueSpring中文文档

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>()

表单数据

若要发送表单数据,可以提供 a 作为正文。请注意, 内容自动设置为 .以下示例演示如何使用:MultiValueMap<String, String>application/x-www-form-urlencodedFormHttpMessageWriterMultiValueMap<String, String>Spring中文文档

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>()

您还可以使用 提供内联表单数据,如以下示例所示:BodyInsertersSpring中文文档

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>()

多部分数据

若要发送分段数据,需要提供其值为 表示部件内容的实例或表示内容的实例和 部件的标题。 提供了一个方便的 API 来准备 多部分请求。以下示例演示如何创建一个:MultiValueMap<String, ?>ObjectHttpEntityMultipartBodyBuilderMultiValueMap<String, ?>Spring中文文档

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-TypeHttpMessageWriterResourceMediaTypepartSpring中文文档

准备好 a 后,将其传递给 通过该方法,如以下示例所示:MultiValueMapWebClientbodySpring中文文档

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>()

如果包含至少一个非值,则也可以 表示常规表单数据(即 ),您不需要 将 设置为 。使用 时总是如此,这确保了包装器。MultiValueMapStringapplication/x-www-form-urlencodedContent-Typemultipart/form-dataMultipartBodyBuilderHttpEntitySpring中文文档

作为替代方法,您还可以提供多部分内容, inline-style,通过内置 ,如以下示例所示:MultipartBodyBuilderBodyInsertersSpring中文文档

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

要按顺序流式传输分段数据,可以通过对象提供分段内容。PartEventSpring中文文档

您可以通过 连接从方法返回的流,并创建对 这。Flux::concatWebClientSpring中文文档

例如,此示例将 POST 包含表单域和文件的多部分表单。Spring中文文档

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()

在服务器端,通过其他服务接收或可以中继到其他服务的对象 通过 .PartEvent@RequestBodyServerRequest::bodyToFlux(PartEvent.class)WebClientSpring中文文档