如何使用 UriComponentsBuilder 构建 URL,其中参数是值列表
How build a URL with UriComponentsBuilder where a parameter is a list of values
我正在尝试使用 UriComponentsBuilder 构建 URI,我有一个值为列表的参数。
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl("https://example/endpoint")
.queryParam("client_id", clientId)
.queryParam("scope", list) //[scope1, scope2, scope3]
.queryParam("redirect_uri", redirectUri);
return uriComponentsBuilder.build(false).encode().toUriString();
输出:
https://example/endpoint?client_id=clientId&scope=scope1&scope=scope2&scope=scope3&redirect_uri=https://redirect
这是不正确的我对列表的每个值都有一个范围参数scope=scope1&scope=scope2&scope=scope3
预期结果应该是:
https://example/endpoint?client_id=clientId&scope=scope1+scope2+scope3&redirect_uri=https://redirect
将列表值映射到参数的正确方法是什么?
你可以
.queryParam("scope", String.join("+",list))
我正在尝试使用 UriComponentsBuilder 构建 URI,我有一个值为列表的参数。
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl("https://example/endpoint")
.queryParam("client_id", clientId)
.queryParam("scope", list) //[scope1, scope2, scope3]
.queryParam("redirect_uri", redirectUri);
return uriComponentsBuilder.build(false).encode().toUriString();
输出:
https://example/endpoint?client_id=clientId&scope=scope1&scope=scope2&scope=scope3&redirect_uri=https://redirect
这是不正确的我对列表的每个值都有一个范围参数scope=scope1&scope=scope2&scope=scope3
预期结果应该是:
https://example/endpoint?client_id=clientId&scope=scope1+scope2+scope3&redirect_uri=https://redirect
将列表值映射到参数的正确方法是什么?
你可以
.queryParam("scope", String.join("+",list))