RestTemplate Post Example

By | | Updated : 2021-10-16 | Viewed : 61 times

RestTemplate Post Example

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 postForObject() and postForEntity() methods are required. So we will be exploring how to use postForObject() and postForEntity() methods in Spring Boot.

Resttemplate Postforobject

Notice below given code snippet for postForObject() where the resource is being passed from one another.

restTemplate Client: postForObject()

   //Using PostForObject
    public CustomerDetails createCustDetailsPostForObject(CustomerDetails customerDetails) {
        return restTemplate.postForObject("http://localhost:8082/restTemplateServer/api/customerDetails", customerDetails, CustomerDetails.class);
    }
restTemplate Server: postForObject()

   @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 more parameters.

Postforobject With Query Parameters

restTemplate Client: postForObject() with Params

//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);
    }
restTemplate Server: postForObject() with Params
   @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

restTemplate Client: postForEntity()
    //Using PostForEntity without Params
    public CustomerDetails createCustDetailsPostForEntity(CustomerDetails customerDetails) {
        return restTemplate.postForEntity("http://localhost:8082/restTemplateServer/api/customerDetails", customerDetails, CustomerDetails.class).getBody();
    }
restTemplate Server: postForEntity()
    @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.

Leave A Reply