Home  >  Forum  >  Spring
Post New Query

RestTemplate postForObject() vs exchange()



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



Replied on November 19, 2020

RestTemplate.postForObject()

The postForObject method creates a new resource by posting the given object to given url or URI template using HTTP POST method. Request object is the payload to post and we can also use request as HttpEntity that helps to add additional HTTP headers. To post data on URI template using postForObject method, we can pass URI variables as Map and Object Varargs.

Find the method declarations.
1.
T postForObject(URI url, Object request, Class<T> responseType)
2.
T postForObject(String url, Object request, Class<T> responseType, Map<String,?> uriVariables) 
3.
T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables) 

Example:
Client code

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

URI uri = new URI("http://localhost:8080/employee");
Employee objEmp = new Employee();
objEmp.setName("Krishna");
objEmp.setCity("Noida");

HttpEntity<Employee> httpEntity = new HttpEntity<>(objEmp, headers);

RestTemplate restTemplate = new RestTemplate();
Employee employee = restTemplate.postForObject(uri, httpEntity, Employee.class);

System.out.println("Id: " + employee.getEmpId());

Server Code

@PostMapping(value = "employee")
public ResponseEntity<Employee> addEmployee(@RequestBody Employee employee, UriComponentsBuilder builder) {

empService.addEmployee(employee);

HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/employee/{id}").buildAndExpand(employee.getEmpId()).toUri());
return new ResponseEntity<Employee>(employee, headers, HttpStatus.CREATED);
}


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
URI uri = new URI("http://localhost:8080/employee");
HttpEntity<Employee> httpEntity = new HttpEntity<Employee>(objEmp, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Employee> responseEntity = restTemplate.exchange(uri, HttpMethod.POST, httpEntity, Employee.class); 

Server code
@PostMapping(value = "employee")
public ResponseEntity<Employee> addEmployee(@RequestBody Employee employee, UriComponentsBuilder builder) {
   ------





Write Answer








©2020 concretepage.com | Privacy Policy | Contact Us