本文介绍了IllegalArgumentException:RestTemplate没有足够的变量值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图像这样使用RestTemplate执行URL -

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?

更新: -

我也试过这个网址,它对我不起作用。我刚刚用 {{@ resourceId}}

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";

更新-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)


推荐答案

似乎 RestTemplate 有一种忽略 {...} 的方法。相反,从 String 值生成 URI (不包含双 {} )。

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

并使用重载的 getForObject 方法一个 URI 。

and use the overloaded getForObject method which takes a URI.

这篇关于IllegalArgumentException:RestTemplate没有足够的变量值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 10:42