我想通过 RestTemplate 发送请求。但是我的网址有大括号('{'、'}'(,因此我有例外:"没有足够的变量值可用于扩展......"。
我尝试通过 uri 来做
UriComponentsBuilder builder = UriComponentsBuilder.fromPath(url);
UriComponents uriComponents = builder.build();
URI uri = uriComponents.toUri();
但是我得到了新的异常:"协议= https主机=空"。
如何使用我的 URL 发送请求?在 URL 中必须大括号。
我的代码:
String url = "https://api.vk.com/method/execute?code=return[API.users.search({"count":1})];&access_token...
String result = restTemplate.getForObject(url, String.class);
下面的代码使用UriComponentsBuilder
加密uri,使用RestTemplate
添加查询参数,并设置HttpHeaders
如果有的话。
public HttpEntity<String> getEntityByUri() {
String req = "https://api.vk.com/method/execute";
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(req)
.queryParam("code",
"return[API.users.search({"count":1})]");
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.ALL));
HttpEntity<String> httpEntity = new HttpEntity<String>(headers);
return new RestTemplate().exchange(builder.build().encode().toUri(), HttpMethod.GET, httpEntity, String.class);
}
希望这有帮助,祝你好运!