CXF Web 客户端中的动态查询参数

use*_*230 3 java apache spring cxf

我想在运行时添加查询参数,传统上我们用 url?a=b&b=c&so 来做。但我不知道参数长度,所以我想动态添加查询参数如何做到这一点?

Pat*_*ick 5

您可以将任意数量的参数添加到CXF Web客户端使用WebClient.query(String, Object...)。例如,如果您有一个参数映射,您可以执行以下操作:

    Map<String, String> params = new HashMap<>();
    params.put("foo", "hello");
    params.put("bar", "world");

    WebClient webClient = WebClient.create("http://url"); 
    for (Entry<String, String> entry : params.entrySet()) {
        webClient.query(entry.getKey(), entry.getValue());
    }

    Response res = webClient.get(); 
Run Code Online (Sandbox Code Playgroud)

这将导致一个 GET 请求 /url?foo=hello&bar=world