Not enough variable values available to expand ‘variableName’ error -SOLUTION

Vivek Singh
2 min readMar 2, 2022

--

This error is related to invoking a RestTemplate get endpoint which includes a List in the Query Parameters.

Oh man. I struggled to get this done. So here is the implementation so that nobody else goes through the same pain.

Objective : Invoke a Get endpoint which accepts multiple query parameters including list of objects.

Consider this is the API you’re trying to invoke :

@RequestMapping(value = "user/get", method = RequestMethod.GET)
public HttpEntity<List<String>> getAllUserData (
@RequestParam(value = "userId", required = true) Integer userId,
@RequestParam(value = "emp", required = true) List<Short> emp,
@RequestParam(value = "rollId", required = true) String rollId

{
LOGGER.info("Get User Data endpoint called");
// Do Something - Your business logic
}

URL of the endpoint being invoked :
http://localhost:8080/user/get?userId={userId}&emp={emp}&rollId={rollId}

URL endpoint with example data(Success) :

http://localhost:8080/user/get?userId=1&emp=1,2&rollId=1

URL endpoint with example data(UnSuccessfull) :

http://localhost:8080/user/get?userId=1&emp=[1,2]&rollId=1

Issue : The list needs to be passed as comma separate string like “1,2” instead of [1,2] while invoking the endpoint.

Now let’s jump to implementation :

String baseUrl = "http://localhost:8080/user/get?userId={userId}&emp={emp}&rollId={rollId}";
LOGGER.info("baseurl : "+baseUrl);

RestTemplate restTemplate = new RestTemplate();
HttpEntity<String> request = new HttpEntity<>(null);
Map<String, Object> params = new HashMap<>();
Integer userId = 1;
String rollId = "1";
List<Short> emp =[1,2];// this array type paranthesis will not work. Because RestTemplate accepts the list to be passed as comma separated string. Like "1,2". So we will change emp from [1,2] to "1,2" using below java codeString commaSeparatedStatus = status.stream()
.map(i -> i.toString())
.collect(Collectors.joining(", "));
// "1,2"
params.put("userId", userId);
params.put("emp", emp);
params.put("rollId", rollId);
try {
ResponseEntity<List> response = restTemplate.exchange(baseUrl, HttpMethod.GET, request,
List.class, params);
if (response.getStatusCode() == HttpStatus.OK) {
LOGGER.info("Successful endpoint call");
} else {
LOGGER.info("Unsuccessful endpoint call");
}
}

So the main challenge while calling an endpoint which need List as query parameters is how the list is passed when you invoke the endpoint. If you pass a list directly like [1,2], this results in an error. So changing the list to command separated string like “1,2” is the solution.

Happy learning. Cheers :)

You can check my other cool tech articles and follow to get one inch closer to understanding tech.

--

--

Vivek Singh
Vivek Singh

Written by Vivek Singh

Software Developer. I write about Full Stack, NLP and Blockchain. Buy me a coffee - buymeacoffee.com/viveksinless

No responses yet