Issue
At backend side I have REST controller with POST method:
@RequestMapping(value = "/save", method = RequestMethod.POST)
public Integer save(@RequestParam String name) {
//do save
return 0;
}
How can I create request using href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-client" rel="nofollow noreferrer">WebClient with request parameter?
WebClient.create(url).post()
.uri("/save")
//?
.exchange()
.block()
.bodyToMono(Integer.class)
.block();
Solution
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:
WebClient.create().get()
.uri(builder -> builder.scheme("http")
.host("example.org").path("save")
.queryParam("name", "spring-framework")
.build())
.retrieve()
.bodyToMono(String.class);
Answered By - Brian Clozel
Answer Checked By - Robin (JavaFixing Admin)