How to create request with parameters with webflux Webclient?(如何使用WebFlux WebClient创建带参数的请求?)
问题描述
在后端我有一个带有POST方法的REST控制器:
@RequestMapping(value = "/save", method = RequestMethod.POST)
public Integer save(@RequestParam String name) {
//do save
return 0;
}
如何使用WebClient和请求参数创建请求?
WebClient.create(url).post()
.uri("/save")
//?
.exchange()
.block()
.bodyToMono(Integer.class)
.block();
推荐答案
在创建URI时存在许多编码挑战。为了在编码部分保持正确的同时获得更大的灵活性,WebClient
为URI提供了一个基于构建器的变体:
WebClient.create().get()
.uri(builder -> builder.scheme("http")
.host("example.org").path("save")
.queryParam("name", "spring-framework")
.build())
.retrieve()
.bodyToMono(String.class);
这篇关于如何使用WebFlux WebClient创建带参数的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!