请求体

请求体可以从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>作为请求体。请注意,内容会自动设置为application/x-www-form-urlencodedFormHttpMessageWriter。以下示例展示了如何使用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>()

多部分数据

要发送多部分数据,您需要提供一个MultiValueMap<String, ?>,其值可以是代表部分内容的Object实例,或者代表部分内容和头信息的HttpEntity实例。MultipartBodyBuilder提供了一个方便的API来准备多部分请求。以下示例展示了如何创建一个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); // 来自服务器请求的部分

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) // 来自服务器请求的部分
}

val parts = builder.build()

在大多数情况下,您不必为每个部分指定Content-Type。内容类型会根据选择的HttpMessageWriter自动确定序列化它,或者在Resource的情况下,根据文件扩展名确定。如果需要,您可以通过重载的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以内联方式提供多部分内容,如下例所示:

  • 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

要按顺序流式传输多部分数据,您可以通过PartEvent对象提供多部分内容。

  • 通过FormPartEvent::create可以创建表单字段。

  • 通过FilePartEvent::create可以创建文件上传。

您可以通过Flux::concat连接从方法返回的流,并为WebClient创建请求。

例如,此示例将POST一个包含表单字段和文件的多部分表单。

  • 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中继到另一个服务。