Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create request with parameters with webflux Webclient?

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 WebClient with request parameter?

WebClient.create(url).post()
    .uri("/save")
    //?
    .exchange()
    .block()
    .bodyToMono(Integer.class)
    .block();
like image 870
Zufar Muhamadeev Avatar asked Feb 16 '18 14:02

Zufar Muhamadeev


1 Answers

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);
like image 110
Brian Clozel Avatar answered Oct 12 '22 22:10

Brian Clozel