【发布时间】:2014-12-18 01:25:17
【问题描述】:
我正在尝试像这样使用 RestTemplate 执行 URL -
public static void main(String[] args) throws UnsupportedEncodingException {
RestTemplate restTemplate = new RestTemplate();
String url = "http://ptr.vip.host.com/pss/repositories/pssdb/branches/main/query/Service[@alias="
+ "hello"
+ "].serviceInstances.runsOn{@resourceId}?allowScan=true&limit=10000&skip=0";
try {
String response = restTemplate.getForObject(url, String.class);
System.out.println(response);
} catch (RestClientException ex) {
ex.printStackTrace();
}
}
但是每次我遇到这样的错误 -
Exception in thread "main" java.lang.IllegalArgumentException: Not enough variable values available to expand '@resourceId'
at org.springframework.web.util.UriComponents$VarArgsTemplateVariables.getValue(UriComponents.java:272)
我做错了什么以及如何解决?
更新:-
我也尝试过使用此网址,但它对我不起作用。我刚刚用{{@resourceId}}替换了{@resourceId}
String url = "http://ptr.vip.host.com/pss/repositories/pssdb/branches/main/query/Service[@alias="
+ "hello"
+ "].serviceInstances.runsOn{{@resourceId}}?allowScan=true&limit=10000&skip=0";
Update-2
这里是代码 -
try {
UriComponentsBuilder builder = UriComponentsBuilder
.fromPath("http://ptr.vip.str.host.com/pss/repositories/pssdb/branches/main/query/Service[@alias="
+ "hello"
+ "].serviceInstances.runsOn{@resourceId}?allowScan=true&limit=10000&skip=0");
UriComponents uriComponents = builder.build();
URI uri = uriComponents.toUri();
String response = restTemplate.getForObject(uri, String.class);
System.out.println(response);
} catch (RestClientException ex) {
ex.printStackTrace();
}
错误是 -
Exception in thread "main" java.lang.IllegalArgumentException: URI is not absolute
at java.net.URI.toURL(URI.java:1095)
at org.springframework.http.client.SimpleClientHttpRequestFactory.createRequest(SimpleClientHttpRequestFactory.java:109)
at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:76)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:479)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:460)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:228)
【问题讨论】:
-
{@resourceId}是 uri 变量,还是字面上出现在 uri 中? (我已标记为收藏,稍后我会回来查看。) -
@SotiriosDelimanolis
{@resourceId}应该按原样出现在 url 中,这意味着它将按原样出现在 uri 中。 -
我相信您需要转义
{和}因为它们标记了 URI 变量。如果我是对的,它应该是{{@resourceId}}。我会尽快调查的。 -
@SotiriosDelimanolis 我试过了,但它对我不起作用。我已经用我尝试过的 URL 更新了问题。还有其他想法吗?
标签: java url resttemplate illegalargumentexception