我有一个Spring Boot 2.2.5应用程序调用此REST端点
@RequestMapping(path = "/usable/search")
public List<Provider> findUsable(
@RequestParam(name = "country-id", required = false) Integer countryId,
@RequestParam(name = "network-ids[]", required = false) List<Integer> networkIds,
@RequestParam(name = "usages[]") Set<Usage> usages)
我的来电
val url = clientUri.toString() + configuration.getUrl().getUseableProvidersForNetworks(); % 'providers/usable/search?'
val builder = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("usages[]", USAGES)
.queryParam("network-ids[]", networkIds);
val response =
restTemplate.exchange(
builder.toUriString(),
HttpMethod.GET,
entity,
new ParameterizedTypeReference<List<Provider>>() {});
其中
USAGES
是Set<Usage>
,networkIds
是List<Integer>
。RestTemplate由创建
RestTemplate eurekaRestTemplate() {
val requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setReadTimeout(TIMEOUT);
requestFactory.setConnectTimeout(TIMEOUT);
return new RestTemplate(new BufferingClientHttpRequestFactory(requestFactory));
}
问题:
[]
被双重编码为%255B%255D
,最后我调用了providers/usable/search?usages%255B%255D=MESSAGE_DELIVERY&network-ids%255B%255D=2145118475&network-ids%255B%255D=-1536358371
这显然是行不通的;服务器未命中
usages[]
。它与[]
而不是%255B%255D
配合使用curl。我该如何避免呢?
我知道this issue和the docs,但这还没有帮助。
最佳答案
我认为您不应该使用[]。我的意思是,没有必要使用“ []”来命名参数。
为什么要这么做?
val builder = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("usages", USAGES)
.queryParam("network-ids", networkIds);
完美适用于:
public List<Provider> findUsable(
@RequestParam(name = "country-id", required = false) Integer countryId,
@RequestParam(name = "network-ids", required = false) List<Integer> networkIds,
@RequestParam(name = "usages") Set<Usage> usages)
希望能帮助到你!
提示:在URL上使用数组/集合参数时要小心,必须考虑URL长度限制