对于最新稳定版本,请使用 Spring Framework 7.0.6spring-doc.cadn.net.cn

请求体

请求体可以从 ReactiveAdapterRegistry 所处理的任意异步类型进行编码,例如以下示例所示的 Mono 或 Kotlin 协程的 Deferredspring-doc.cadn.net.cn

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-doc.cadn.net.cn

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 快捷方法,如下例所示:spring-doc.cadn.net.cn

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> 作为请求体。请注意,内容类型会由 application/x-www-form-urlencoded 自动设置为 FormHttpMessageWriter。以下示例展示了如何使用 MultiValueMap<String, String>spring-doc.cadn.net.cn

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 内联提供表单数据,如下例所示:spring-doc.cadn.net.cn

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)数据,您需要提供一个 MultiValueMap<String, ?>,其值可以是表示部分内容的 Object 实例,也可以是表示部分内容及其头部信息的 HttpEntity 实例。MultipartBodyBuilder 提供了一个便捷的 API 来构建多部分请求。以下示例展示了如何创建一个 MultiValueMap<String, ?>spring-doc.cadn.net.cn

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 类型,则根据文件扩展名自动确定。如有必要,您可以通过重载的构建器 MediaType 方法之一,显式地为每个部分指定要使用的 partspring-doc.cadn.net.cn

一旦准备好 MultiValueMap,将其传递给 WebClient 最简单的方式是通过 body 方法,如下例所示:spring-doc.cadn.net.cn

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 包装器。spring-doc.cadn.net.cn

作为 MultipartBodyBuilder 的替代方案,您也可以通过内置的 BodyInserters 以内联方式提供 multipart 内容,如下例所示:spring-doc.cadn.net.cn

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

要按顺序流式传输多部分数据,您可以通过 PartEvent 对象提供多部分内容。spring-doc.cadn.net.cn

你可以通过 Flux::concat 连接从方法返回的流,并为 WebClient 创建一个请求。spring-doc.cadn.net.cn

例如,此示例将提交一个包含表单字段和文件的 multipart 表单。spring-doc.cadn.net.cn

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@RequestBody 接收的 ServerRequest::bodyToFlux(PartEvent.class) 对象可以通过 WebClient 转发到另一个服务。spring-doc.cadn.net.cn