此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10! |
请求正文
请求正文可以从由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>
作为身体。请注意,
content 自动设置为application/x-www-form-urlencoded
通过FormHttpMessageWriter
.以下示例演示如何使用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); // 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
通过重载的
架构工人part
方法。
一次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
value,这也可以
表示常规形式数据(即application/x-www-form-urlencoded
),您不必
将Content-Type
自multipart/form-data
.使用MultipartBodyBuilder
,这确保了HttpEntity
包装纸。
作为替代方案MultipartBodyBuilder
,还可以提供多部分内容,
inline-style,通过内置的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
.
例如,此示例将发布包含表单字段和文件的多部分表单。
-
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()
在服务器端,PartEvent
通过@RequestBody
或ServerRequest::bodyToFlux(PartEvent.class)
可以中继到另一个服务
通过WebClient
.