Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

java - IllegalArgumentException: Not enough variable values available with RestTemplate?

I am trying to execute URL using RestTemplate like this -

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}}

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

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It doesn't seem like RestTemplate has a means of ignoring {...}. Instead, generate a URI from your String value (without the 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 getForObject method which takes a URI.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...