本文介紹了如何使用WebFlux WebClient創(chuàng)建帶參數(shù)的請求?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
在后端我有一個帶有POST方法的REST控制器:
@RequestMapping(value = "/save", method = RequestMethod.POST)
public Integer save(@RequestParam String name) {
//do save
return 0;
}
如何使用WebClient和請求參數(shù)創(chuàng)建請求?
WebClient.create(url).post()
.uri("/save")
//?
.exchange()
.block()
.bodyToMono(Integer.class)
.block();
推薦答案
在創(chuàng)建URI時存在許多編碼挑戰(zhàn)。為了在編碼部分保持正確的同時獲得更大的靈活性,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創(chuàng)建帶參數(shù)的請求?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,