RestTemplate getForObject() vs getForEntity()




Asked on November 13, 2020
What is difference between Spring RestTemplate.getForObject() and RestTemplate.getForEntity() ?


Replied on November 14, 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.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) {
  ------







Write Answer











©2024 concretepage.com | Privacy Policy | Contact Us