According to wikipedia, Representational state transfer (REST) or RESTful Web services are one way of providing interoperability between computer systems on the Internet.

With RestTemplate class we can make any REST calls to different applications. In this blog we are going to discuss about 4 main HTTP request these are GET, POST, PUT, DELETE. Before you go through this blog i would stronly recommend to go through this link to get description about RestTemplate methods. Below are some example of REST calls.

1. GET request with parameters

To make GET request we can use either getForObject or getForEntity method . Below is a example of GET request using getForObject method, Here to generate URL we have used Spring's UriComponentsBuilder class and to parse response from that URL we have used WalletListDTO class.

String transactionUrl = "http://localhost:8080/api/v1/transactions" ; UriComponentsBuilder builder = UriComponentsBuilder . fromUriString ( transactionUrl ) // Add query parameter . queryParam ( "pageNumber" , "1" ) . queryParam ( "walletId" , "2323JK" ) . queryParam ( "pageSize" , "10" ); RestTemplate restTemplate = new RestTemplate (); WalletListDTO response = restTemplate . getForObject ( builder . toUriString (), walletListDTO . class );

2. POST Request with JSON data

To make POST request we have used postForObject method and to send JSON data we have used JSONObject class which is available in org.json.* package.

String walletBalanceUrl = "http://localhost:8080/api/v1/wallets/balance" ; HttpHeaders httpHeaders = new HttpHeaders (); httpHeaders . set ( "Content-Type" , "application/json" ); JSONObject json = new JSONObject (); json . put ( "walletId" , "6337637DL" ); HttpEntity < String > httpEntity = new HttpEntity < String > ( json . toString (), httpHeaders ); RestTemplate restTemplate = new RestTemplate (); String response = restTemplate . postForObject ( walletBalanceUrl , httpEntity , String . class ); JSONObject jsonObj = new JSONObject ( response ); String balance = jsonObj . get ( "data" ). toString ();

3. PUT Request with parameter

To make a PUT request we have used put method, Here we have send data in parameters.

String url = "http://localhost:8080/api/v2/wallets/list" ; UriComponentsBuilder builder = UriComponentsBuilder . fromUriString ( url ) // Add query parameter . queryParam ( "walletId" ,"455445DL " ); RestTemplate restTemplate = new RestTemplate (); restTemplate . put ( builder . toUriString (), null );

4. Delete with paramater

We have used delete method to make DELETE request.

String walletUrl = "http://localhost:8080/api/v1/wallets" ; UriComponentsBuilder = builder = UriComponentsBuilder . fromUriString ( walletUrl ) . queryParam ( "walletId" , "67880KL" ); RestTemplate restTemplate = new RestTemplate (); restTemplate . delete ( builder . toUriString (), null );

5. Any Http Request with exhange or execute method

With exchange or execute method we can make any REST call it can be either PUT, POST, DELETE, GET, HEAD, OPTIONS etc..

Lets take an example of GET request with exchange method which will fetch list of country object.

String countryJsonUrl = "http://localhost:8080/api/v1/countries" ; ResponseEntity < List < Country >> response = restTemplate . exchange ( countryJsonUrl , HttpMethod . GET , null , new ParameterizedTypeReference < List < Country >> () {}); List < Country > country = response . getBody ();

Thanks