RestTemplate postForEntity() vs exchange()




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



Replied on November 20, 2020

RestTemplate.postForEntity()

The postForEntity method creates new resource by posting the given object to the given URI template using HTTP POST method. The postForEntity method returns instance of ResponseEntity using which we can fetch the information about HTTP status, URI of newly created resource, response content body etc. The postForEntity method accepts URI template, object to post, response type.

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

Example:
Client code

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

String url = "http://localhost:8080/employee/{profile}/{tech}";
Employee objEmp = new Employee();
objEmp.setName("Krishna");
objEmp.setCity("Noida");

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

String profile = "Developer";
String technology = "Java";
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Employee> responseEntity = restTemplate.postForEntity(url, requestEntity, Employee.class, profile, technology);

System.out.println("Status Code: " + responseEntity.getStatusCode());
System.out.println("Id: " + responseEntity.getBody().getEmpId());
System.out.println("Location: " + responseEntity.getHeaders().getLocation());

Server code
@PostMapping(value = "employee/{profile}/{tech}")
public ResponseEntity<Employee> addEmployee(@RequestBody Employee employee, @PathVariable("profile") String profile,
@PathVariable("tech") String technology, UriComponentsBuilder builder) {
   ------



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










©2024 concretepage.com | Privacy Policy | Contact Us