如何使用WebFlux WebClient创建带参数的请求? [英] How to create request with parameters with webflux Webclient?

查看:21
本文介绍了如何使用WebFlux WebClient创建带参数的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在后端我有一个带有POST方法的REST控制器:

@RequestMapping(value = "/save", method = RequestMethod.POST)
public Integer save(@RequestParam String name) {
   //do save
   return 0;
}

如何使用WebClient和请求参数创建请求?

WebClient.create(url).post()
    .uri("/save")
    //?
    .exchange()
    .block()
    .bodyToMono(Integer.class)
    .block();

推荐答案

在创建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);

这篇关于如何使用WebFlux WebClient创建带参数的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆