Overview
RestTemplate reduces boilerplate code and enforces RESTful principles.
In this post, we will discuss different ways in which we can use RestTemplate
- HTTP GET call
- HTTP POST call
- HTTP Exchange call
- HTTP DELETE call
GET call
Get entry point has two methods
- getForEntity: returns ResponseEntity which can be used to get ResponseStatus code and other parameters.
RestTemplate restTemplate = new RestTemplate();
String apiUrl = "http://dummy.restapiexample.com/api/v1/employees";
//get Response Entity
ResponseEntity<String> response = restTemplate.getForEntity(apiUrl , String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
- getForObject: convert and returns Object passed to it as the parameter. In case RestTemplate is not able to parse will throw RestClientException.
//get Object
User user = restTemplate.getForObject(apiUrl , User.class);
assertThat(user.getName(), notNullValue());
assertThat(user.getId(), is(234L));
POST call
Post entry point has the following methods
- postForObject: convert and returns Object passed to it as the parameter. In case RestTemplate is not able to parse will throw RestClientException.
String apiUrl = "http://dummy.restapiexample.com/api/v1/create";
RestTemplate restTemplate = new RestTemplate();
HttpEntity<User> request = new HttpEntity<>(new User("Test"));
// post for Object, returns User Object
User user = restTemplate.postForObject(apiUrl, request, User.class);
assertThat(user, notNullValue());
assertThat(user.getName(), is("Temp"));
- postForLocation: returns the Location of that newly created Resource.
// post for Location, returns URI
URI location = restTemplate.postForLocation(apiUrl, request);
assertThat(location, notNullValue());
- postForEntity: Can be used to submit a Form and return ResponseEntity which can be used to get ResponseStatus code and other parameters
// post for Entity can be used to submit a Form
//setting header to "application/x-www-form-urlencoded"
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
// converting form variable to Map
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
map.add("salary", "123");
map.add("name", "Test");
// finally build Request
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
ResponseEntity<String> response = restTemplate.postForEntity(apiUrl, request , String.class);
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
Exchange call
Exchange entry point gives a more generic way of using HTTP calls.
We will see how we can do POST call using exchange method
String apiUrl = "http://dummy.restapiexample.com/api/v1/create";
RestTemplate restTemplate = new RestTemplate();
HttpEntity<User> request = new HttpEntity<>(new User("Temp"));
ResponseEntity<User> response = restTemplate
.exchange(apiUrl, HttpMethod.POST, request, User.class);
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
DELETE
String entityUrl = "http://dummy.restapiexample.com/api/v1/update/2"
restTemplate.delete(entityUrl);
Common Methods
DELETE => delete(url, java.lang.Object...)
GET => getForObject(url, responseType, urlVariables(Map))
getForEntity(url, responseType, urlVariables)
POST => postForLocation(url, responseType, urlVariables)
postForObject(url, objToPost, responseType, urlVariables(Map))
PUT => put(url, objToPost, urlVariables)
All methods throws RestClientException
Note
- Internally the template uses HttpMessageConverter instances to convert HTTP messages to and from POJOs.
- By default, the RestTemplate relies on standard JDK facilities to establish HTTP connections. You can switch to use a different HTTP library.
- Such as Apache HttpComponents, Netty, and OkHttp through the HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) property.
- More detail on Java Doc can be found here.
RestTemplate restTemplate = new RestTemplate();
String apiUrl = "http://dummy.restapiexample.com/api/v1/employees";
//get Response Entity
ResponseEntity<String> response = restTemplate.getForEntity(apiUrl , String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
0 Comments