RestTemplate getForEntity() vs exchange()




Asked on November 14, 2020
What is difference between Spring RestTemplate getForEntity() and exchange() ?



Replied on November 19, 2020

RestTemplate.getForEntity()


The getForEntity method retrieves resources from the given URI or URL templates. It returns response as ResponseEntity using which we can get response status code, response body etc. To fetch data on the basis of some key properties, we can send them as path variables. We need to use URI template and pass a Map or Object varargs to getForEntity method to expand it on URI template.

1.
ResponseEntity<T> getForEntity(URI url, Class<T> responseType) 

2.
ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) 

3.
ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String,?> uriVariables)


Example:

Client code

String url = "http://localhost:8080/employee/{profile}/{tech}";
Map<String, String> map = new HashMap<>();
map.put("profile", "Developer");
map.put("tech", "Java");
ResponseEntity<Employee[]> responseEntity = restTemplate.getForEntity(url, Employee[].class, map); 

In the above code the URI variables has been passed as Map. If we want to use object varargs in the above code, we can do it as following.

String profile = "Developer";
String tech = "Java";
ResponseEntity<Employee[]> responseEntity = restTemplate.getForEntity(url, Employee[].class, profile, tech); 

Now find the Server code.

@GetMapping("employee/{profile}/{tech}")
public ResponseEntity<List<Employee>> getEmployeesByProfileNTech(@PathVariable("profile") String profile,
@PathVariable("tech") String technology) {
  ------


RestTemplate.exchange()

The exchange method executes the request of any HTTP method and returns ResponseEntity instance. The exchange method can be used for HTTP DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, TRACE methods. Using exchange method we can perform CRUD operation i.e. create, read, update and delete data. 

The exchange() method can be used with variety of parameters.

1. RequestEntity + responseType
ResponseEntity<T> exchange(RequestEntity<?> requestEntity, Class<T> responseType)
ResponseEntity<T> exchange(RequestEntity<?> requestEntity, ParameterizedTypeReference<T> responseType)

2. url + HttpMethod + HttpEntity + responseType + uriVariables
ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Map<String,?> uriVariables)
ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables)
ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String,?> uriVariables)
ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Object... uriVariables) 

3. URI + HttpMethod + HttpEntity + responseType
ResponseEntity<T> exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType)
ResponseEntity<T> exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType)


Example:
Client code
String url = "http://localhost:8080/employee/{profile}/{tech}";
Map<String, String> map = new HashMap<>();
map.put("profile", "Developer");
map.put("tech", "Java");
ResponseEntity<Employee[]> responseEntity = 
restTemplate.exchange(url, HttpMethod.GET, httpEntity, Employee[].class, map);

Server code
@GetMapping(value = "employee/{profile}/{tech}")
public ResponseEntity<List<Employee>> getEmployeesByProfileNTech(@PathVariable("profile") String profile,
@PathVariable("tech") String technology) {
 ------





Write Answer










©2024 concretepage.com | Privacy Policy | Contact Us