Java IllegalArgumentException:RestTemplate 没有足够的变量值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27537766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:33:03  来源:igfitidea点击:

IllegalArgumentException: Not enough variable values available with RestTemplate?

javaurlresttemplateillegalargumentexception

提问by john

I am trying to execute URL using RestTemplate like this -

我正在尝试使用这样的 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();
    }
}

But everytime I am getting error like this -

但每次我收到这样的错误 -

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)

What is wrong I am doing and how to fix it?

我在做什么错以及如何解决?

Update:-

更新:-

I tried with this url as well and it didn't worked for me. I just replaced {@resourceId}with {{@resourceId}}

我也试过这个网址,但对我不起作用。我刚换{@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

更新 2

Here is the code -

这是代码 -

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();
}

And the error is -

而错误是——

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)

采纳答案by Sotirios Delimanolis

It doesn't seem like RestTemplatehas a means of ignoring {...}. Instead, generate a URIfrom your Stringvalue (without the double {}).

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

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();

and use the overloaded getForObjectmethod which takes a URI.

并使用getForObject带有URI.

回答by Richard Xue

U should see the soucre of RestTemplate. The method of getForObject need three parameters,like

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

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

The 'url' is the part of url's prefix(in front of '?') and 'urlVariables' is a array of parameters.

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