java 如何使用 webflux Webclient 创建带参数的请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48828603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create request with parameters with webflux Webclient?
提问by Zufar Muhamadeev
At backend side I have REST controller with POST method:
在后端,我有带有 POST 方法的 REST 控制器:
@RequestMapping(value = "/save", method = RequestMethod.POST)
public Integer save(@RequestParam String name) {
//do save
return 0;
}
How can i create request using WebClientwith request parameter?
如何使用带有请求参数的WebClient创建请求?
WebClient.create(url).post()
.uri("/save")
//?
.exchange()
.block()
.bodyToMono(Integer.class)
.block();
回答by Brian Clozel
There are many encoding challenges when it comes to creating URIs. For more flexibility while still being right on the encoding part, WebClient
provides a builder-based variant for the URI:
在创建 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);
回答by Alexis Gamarra
From: https://www.callicoder.com/spring-5-reactive-webclient-webtestclient-examples/
来自:https: //www.callider.com/spring-5-reactive-webclient-webtestclient-examples/
webClient.get()
.uri(uriBuilder -> uriBuilder.path("/user/repos")
.queryParam("sort", "updated")
.queryParam("direction", "desc")
.build())
.header("Authorization", "Basic " + Base64Utils
.encodeToString((username + ":" + token).getBytes(UTF_8)))
.retrieve()
.bodyToFlux(GithubRepo.class);