RestTemplate GET request with request params

Chandra picture Chandra · Aug 26, 2011 · Viewed 42.8k times · Source

I have to call a REST webservice and I am planning to use RestTemplate. I looked at examples on how to make a GET request and they are as shown below.

 String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}", String.class,"42","21");

In my case the RESTful url is something like below. How do I use RestTemplate in this case?

http://example.com/hotels?state=NY&country=USA

So my question would be how do I send request parameters for GET requests?

Answer

chrismarx picture chrismarx · Oct 14, 2011

the placeholders work the same for either type of url, just do

 String result = restTemplate.getForObject("http://example.com/hotels?state={state}&country={country}", String.class,"NY","USA");

or better yet, use a hashmap for real name matching-