RestTemplate Post Example
By AmarSivas | | Updated : 2021-10-16 | Viewed : 218 times

In some types of resource creation, the resource will have to be passed from one microservice to another one in which the resource will be created. So in these case
Table of Contents:
Resttemplate Postforobject
Notice below given code snippet for postForObject() where the resource is being passed from one another.
//Using PostForObject
public CustomerDetails createCustDetailsPostForObject(CustomerDetails customerDetails) {
return restTemplate.postForObject("http://localhost:8082/restTemplateServer/api/customerDetails", customerDetails, CustomerDetails.class);
}
@PostMapping(value = "/customerDetails")
public ResponseEntity<?> createCustomerDetails(@RequestBody CustomerDetails customerDetails) {
CustomerDetails customerDetailsResults = customerDetailsServiceImpl.createCustomerDetails(customerDetails);
return ResponseEntity.created(URI.create("/restTemplateServer/api/customerDetails/" + customerDetails.getId())).body(customerDetailsResults);
}
In this example, we just tried to create the customer. But in some cases, we need to create a child with parent modification. For suppose we want to create the product and with customer modification. In this case, we can use
Postforobject With Query Parameters
//Using PostForObject with Params
public CustomerDetails createProductAndUpdateCustDetails(Long id, String firstName, ProductDetails productDetails) {
Map<String, Object> requestMap = new HashMap();
requestMap.put("id", id);
requestMap.put("firstName", firstName);
return restTemplate.postForObject("http://localhost:8082/restTemplateServer/api/customerDetails/product/{id}/{firstName}", productDetails, CustomerDetails.class, requestMap);
}
@PostMapping(value = "/customerDetails/product/{id}/{firstName}")
public ResponseEntity<?> createOrderAndUpdateCustomerDetails(@RequestBody ProductDetails productDetails,@PathVariable Long id,@PathVariable String firstName) {
CustomerDetails resultedCustomerDetails = customerDetailsServiceImpl.createProductAndUpdateCustomerDetails(id,firstName, productDetails);
return ResponseEntity.created(URI.create("/restTemplateServer/api/customerDetails/" + resultedCustomerDetails.getId())).body(resultedCustomerDetails);
}
So here we can pass the product object and params of customers also can be passed to the other microservice.
Resttemplate PostForEntity
//Using PostForEntity without Params
public CustomerDetails createCustDetailsPostForEntity(CustomerDetails customerDetails) {
return restTemplate.postForEntity("http://localhost:8082/restTemplateServer/api/customerDetails", customerDetails, CustomerDetails.class).getBody();
}
@PostMapping(value = "/customerDetails")
public ResponseEntity<?> createCustomerDetails(@RequestBody CustomerDetails customerDetails) {
CustomerDetails customerDetailsResults = customerDetailsServiceImpl.createCustomerDetails(customerDetails);
return ResponseEntity.created(URI.create("/restTemplateServer/api/customerDetails/" + customerDetails.getId())).body(customerDetailsResults);
}
So the above code will create the customer with the product.
Postforentity Vs Postforobject
The difference between postForObject() and postForEntity(), return type. postForEntity() provide back ResponseEntity where postForObject provide Entity object.
We can use the exchange() method also to send resources to other microservice. we will see that in other tutorials.
Please refer SpringBoot-RestTemplate-Example-Apprepo for postForObject() and postForEntity() methods.