3

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)
john
  • 11,311
  • 40
  • 131
  • 251
  • Is this `{@resourceId}` meant to be a uri variable or is it meant to appear literally in the uri? (I've marked as favorite, I'll check back later.) – Sotirios Delimanolis Dec 18 '14 at 19:32
  • @SotiriosDelimanolis `{@resourceId}` should be present in the url as it is, meaning it will appear literally in the uri as it is. – john Dec 18 '14 at 20:21
  • I believe you need to escape `{` and `}` because they mark URI variables. If I am right, it should be `{{@resourceId}}`. I'll look into it soon. – Sotirios Delimanolis Dec 18 '14 at 20:31
  • @SotiriosDelimanolis I tried that and it didn't work for me. I have updated the question with the URL I have tried. Any other thoughts? – john Dec 20 '14 at 18:59

2 Answers2

7

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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Thanks for suggestion. Can you provide an example, how the String url will looks like in this situation? I tried removing `{}` from @resouceId and ran it, I got error as `java.lang.IllegalArgumentException: URI is not absolute` so I am thinking my String url was wrong. – john Dec 20 '14 at 19:15
  • @user2809564 Exactly the way you had it before we added the second set of `{}`. – Sotirios Delimanolis Dec 20 '14 at 19:17
  • Ohh, I just tried that and didn't work and I still got same error - `java.lang.IllegalArgumentException: URI is not absolute` – john Dec 20 '14 at 19:21
  • @user2809564 Seems like you are using some other URI, not the one in your question or in my answer. Can you edit your question with the full code and stack trace? – Sotirios Delimanolis Dec 20 '14 at 19:23
  • @user2809564 I don't get that at all with Spring 4.0.2. What version of Spring are you on? – Sotirios Delimanolis Dec 20 '14 at 19:32
  • I am using 3.2.1 as - ` org.springframework spring-web 3.2.1.RELEASE ` – john Dec 20 '14 at 19:45
  • 1
    And when I tried using 4.1.3, I got this error - `java.lang.IllegalArgumentException: protocol = http host = null` – john Dec 20 '14 at 19:48
  • @user2809564 Seems like the hostname in the URI cannot be resolved. – Sotirios Delimanolis Dec 20 '14 at 19:50
  • My Env is spring boot 1.3. The answer doesn't work for me, hit error " URI does not specify a valid host name". But it inspired me, and I found below solution for uri: String url = "http://localhost:8080/service"; URI uri = URI.create(url); – Burt Jun 20 '17 at 13:17
  • @user2542004 What `url` value did you use with the sample in the answer? Because `"localhost:8080/service"` works just fine. Regardless, the question is about the `{}` and you don't seem to be working in that context. – Sotirios Delimanolis Jun 20 '17 at 14:18
  • @Sotirios Delimanolis, my code with the sample in the answer which hit error "URI does not specify a valid host name": String requestStr = "11"; String url = "http://localhost:8080/service/requestCreditRate?requestString=" + requestStr; UriComponentsBuilder builder = UriComponentsBuilder.fromPath(url); UriComponents uriComponents = builder.build(); URI uri = uriComponents.toUri(); ResponseEntity entity = new TestRestTemplate().getForEntity(uri, String.class); – Burt Jun 21 '17 at 01:29
  • @user2542004 I can't reproduce that. I get a `MalformedURLException` because you're missing a protocol in your url. – Sotirios Delimanolis Jun 21 '17 at 05:12
  • @SotiriosDelimanolis the protocol of the url is "http://", which is truncated automatically... – Burt Jun 22 '17 at 02:13
  • @user2542004 Automatically truncated _how_ and _by what_? I'm just running the code you provided, which essentially boils down to [this](http://ideone.com/OFwptR). Perhaps you meant to use `fromHttpUrl` to get the exception thrown sooner. – Sotirios Delimanolis Jun 22 '17 at 02:48
  • @SotiriosDelimanolis Auto truncated by stackoverflow. It's confusing. Hope my code capture [here](https://drive.google.com/open?id=0B7VpU4j0uK6uS0JBblNfS092TEE) is clear. – Burt Jun 22 '17 at 07:48
-1

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

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.

Richard Xue
  • 307
  • 2
  • 5