Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this short tutorial, we’ll take a close look at Spring’s RestTemplate exception IllegalArgumentException: Not enough variables available to expand.

First, we’ll discuss in detail the main cause behind this exception. Then, we’ll showcase how to produce it, and finally, how to solve it.

2. The Cause

In short, the exception typically occurs when we’re trying to send JSON data in a GET request.

Simply put, RestTemplate provides the getForObject method to get a representation by making a GET request on the specified URL.

The main cause of the exception is that RestTemplate considers the JSON data encapsulated in the curly braces as a placeholder for URI variables.

Since we don’t provide any values for the expected URI variables, the getForObject method throws the exception.

For example, attempting to send {“name”:”HP EliteBook”} as value:

String url = "http://products.api.com/get?key=a123456789z&criterion={\"name\":\"HP EliteBook\"}";
Product product = restTemplate.getForObject(url, Product.class);

Will simply cause RestTemplate to throw the exception:

java.lang.IllegalArgumentException: Not enough variable values available to expand 'name'

3. Example Application

Now, let’s see an example of how we can produce this IllegalArgumentException using RestTemplate.

To keep things simple, we’re going to create a basic REST API for product management with a single GET endpoint.

First, let’s create our model class Product:

public class Product {

    private int id;
    private String name;
    private double price;

    // default constructor + all args constructor + getters + setters 
}

Next, we’re going to define a spring controller to encapsulate the logic of our REST API:

@RestController
@RequestMapping("/api")
public class ProductApi {

    private List<Product> productList = new ArrayList<>(Arrays.asList(
      new Product(1, "Acer Aspire 5", 437), 
      new Product(2, "ASUS VivoBook", 650), 
      new Product(3, "Lenovo Legion", 990)
    ));

    @GetMapping("/get")
    public Product get(@RequestParam String criterion) throws JsonMappingException, JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Criterion crt = objectMapper.readValue(criterion, Criterion.class);
        if (crt.getProp().equals("name")) {
            return findByName(crt.getValue());
        }

        // Search by other properties (id,price)

        return null;
    }

    private Product findByName(String name) {
        for (Product product : this.productList) {
            if (product.getName().equals(name)) {
                return product;
            }
        }
        return null;
    }

    // Other methods
}

4. Example Application Explained

The basic idea of the handler method get() is to retrieve a product object based on a specific criterion.

The criterion can be represented as a JSON string with two keys: prop and value.

The prop key refers to a product property, so it can be an id, a name, or a price.

As shown above, the criterion is passed as a string argument to the handler method. We used the ObjectMapper class to convert our JSON string to an object of Criterion.

This is how our Criterion class looks:

public class Criterion {

    private String prop;
    private String value;

    // default constructor + getters + setters
}

Finally, let’s try to send a GET request to the URL mapped to the handler method get().

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { RestTemplate.class, RestTemplateExceptionApplication.class })
public class RestTemplateExceptionLiveTest {

    @Autowired
    RestTemplate restTemplate;

    @Test(expected = IllegalArgumentException.class)
    public void givenGetUrl_whenJsonIsPassed_thenThrowException() {
        String url = "http://localhost:8080/spring-rest/api/get?criterion={\"prop\":\"name\",\"value\":\"ASUS VivoBook\"}";
        Product product = restTemplate.getForObject(url, Product.class);
    }
}

Indeed, the unit test throws IllegalArgumentException because we’re attempting to pass {“prop”:”name”,”value”:”ASUS VivoBook”} as part of the URL.

5. The Solution

As a rule of thumb, we should always use a POST request to send JSON data.

However, although not recommended, a possible solution using GET could be to define a String object containing our criterion and provide a real URI variable in the URL.

@Test
public void givenGetUrl_whenJsonIsPassed_thenGetProduct() {
    String criterion = "{\"prop\":\"name\",\"value\":\"ASUS VivoBook\"}";
    String url = "http://localhost:8080/spring-rest/api/get?criterion={criterion}";
    Product product = restTemplate.getForObject(url, Product.class, criterion);

    assertEquals(product.getPrice(), 650, 0);
}

Let’s look at another solution using the UriComponentsBuilder class:

@Test
public void givenGetUrl_whenJsonIsPassed_thenReturnProduct() {
    String criterion = "{\"prop\":\"name\",\"value\":\"Acer Aspire 5\"}";
    String url = "http://localhost:8080/spring-rest/api/get";

    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url).queryParam("criterion", criterion);
    Product product = restTemplate.getForObject(builder.build().toUri(), Product.class);

    assertEquals(product.getId(), 1, 0);
}

As we can see, we used the UriComponentsBuilder class to construct our URI with the query parameter criterion before passing it to the getForObject method.

6. Conclusion

In this quick article, we discussed what causes RestTemplate to throw the IllegalArgumentException: “Not enough variables available to expand”.

Along the way, we walked through a practical example showing how to produce the exception and solve it.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW