Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestTemplate GET request with request params

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?

like image 228
Chandra Avatar asked Aug 26 '11 02:08

Chandra


People also ask

What is difference between getForObject and getForEntity?

For example, the method getForObject() will perform a GET and return an object. getForEntity() : executes a GET request and returns an object of ResponseEntity class that contains both the status code and the resource as an object. getForObject() : similar to getForEntity() , but returns the resource directly.

Is RestTemplate getting deprecated?

RestTemplate will still be used. But in some cases, the non-blocking approach uses much fewer system resources compared to the blocking one.


1 Answers

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-

like image 68
chrismarx Avatar answered Oct 11 '22 08:10

chrismarx