RestTemplate getForObject() vs exchange()




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


Replied on November 19, 2020

RestTemplate.getForObject()

The getForObject method fetches the data for the given response type from the given URI or URL template using HTTP GET method. To fetch data for the given key properties from URL template we can pass Object Varargs and Map to getForObject method.
Find the getForObject method declarations.
1. 
T getForObject(URI url, Class<T> responseType) 
2.
T getForObject(String url, Class<T> responseType, Object... uriVariables)
3.
T getForObject(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");
Employee[] emps = restTemplate.getForObject(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";
Employee[] emps = restTemplate.getForObject(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) {
 ------





Replied on November 19, 2020

Write Answer










©2024 concretepage.com | Privacy Policy | Contact Us