RestTemplate postForLocation() vs exchange()




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



Replied on November 20, 2020

RestTemplate.postForLocation()

The postForLocation method creates a new resource by posting the given object to given URI template. The postForLocation method returns URI as the value of location header for newly created resource. The postForLocation sends data using HTTP POST method. The postForLocation method is useful when we want the only location header of newly created resource. 

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

Example:

Client code

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

String url = "http://localhost:8080/user/article/{cat}/{subCat}";

Map<String, String> map = new HashMap<>();
map.put("cat", "Spring Security UserDetailsService");
map.put("subCat", "Mahesh");

Article objArticle = new Article();
objArticle.setTitle("Spring Security UserDetailsService");
objArticle.setWriter("Mahesh");

HttpEntity<Article> requestEntity = new HttpEntity<>(objArticle, headers);

RestTemplate restTemplate = new RestTemplate();
URI uri = restTemplate.postForLocation(url, requestEntity, map);

System.out.println(uri.getPath());

Server code

@PostMapping(value = "article/{cat}/{subCat}")
public ResponseEntity<Void> addArticle(@RequestBody Article article, @PathVariable("cat") String category,
@PathVariable("subCat") String subCategory, UriComponentsBuilder builder) {

System.out.println("Category: " + category);
System.out.println("SubCategory: " + subCategory);

articleService.addArticle(article);

HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/article/{id}").buildAndExpand(article.getArticleId()).toUri());
return new ResponseEntity<Void>(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) {
   ------




Replied on May 06, 2021
Nice Explanation.


Replied on December 07, 2022
Nice Amrendra.

Write Answer










©2024 concretepage.com | Privacy Policy | Contact Us