How to create request with parameters with webflux Webclient?

感情迁移 提交于 2019-12-23 07:39:16

问题


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();

回答1:


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);



回答2:


From: https://www.callicoder.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);


来源:https://stackoverflow.com/questions/48828603/how-to-create-request-with-parameters-with-webflux-webclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!