【问题标题】:IllegalArgumentException: Not enough variable values available with RestTemplate?IllegalArgumentException:RestTemplate 没有足够的变量值可用?
【发布时间】: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


【解决方案1】:

RestTemplate 似乎没有办法忽略{...}。相反,从您的 String 值生成一个 URI(没有双 {})。

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";
UriComponentsBuilder builder = UriComponentsBuilder.fromPath(url);
UriComponents uriComponents = builder.build();
URI uri = uriComponents.toUri();

并使用采用URI 的重载getForObject 方法。

【讨论】:

  • 感谢您的建议。您能否提供一个示例,在这种情况下 String url 会是什么样子?我尝试从@resouceId 中删除{} 并运行它,我收到java.lang.IllegalArgumentException: URI is not absolute 的错误,所以我认为我的字符串网址是错误的。
  • @user2809564 与我们添加第二组{} 之前的完全一样。
  • 哦,我刚刚尝试过,但没有成功,我仍然遇到同样的错误 - java.lang.IllegalArgumentException: URI is not absolute
  • @user2809564 似乎您使用的是其他 URI,而不是您的问题或我的答案中的那个。你能用完整的代码和堆栈跟踪编辑你的问题吗?
  • 当我尝试使用 4.1.3 时,我得到了这个错误 - java.lang.IllegalArgumentException: protocol = http host = null
【解决方案2】:

你应该看到 RestTemplate 的源代码。 getForObject 方法需要三个参数,如

public <T> T getForObject(String url, Class<T> responseType, Object... urlVariables)

'url'是url前缀的一部分('?'前面),'urlVariables'是一个参数数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 2011-04-18
    • 1970-01-01
    • 2014-03-16
    相关资源
    最近更新 更多