"Welcome To Ashok IT" "Spring Boot & Microservices" Topic : RestClient Development Date : 07/01/2025 (Session - 100,101) _____________________________________________________________________________________________________________________________ Rest Client =========== * So far we developed the Rest API's and also how to generate documentation for Rest API's and also tested the Rest API's by using POSTMAN Tool and Swagger Documentation. * RestClient is a Java Program which is used to consume services from Rest API (or) Rest Client is used to access the Rest API's. * For Business-to-Business we will develop a program to access Rest API. * The Program which is accessing Rest API can be called as "Rest Client". * Inorder to develop Rest Client, We need Rest API Documentation. * Rest API documentation will be provided through Swagger and with out knowing the API Details Of Rest API we can't develop the RestClient Application. * We can develop the RestClient in Spring by using three techniques 1) HttpClient 2) RestTemplate 3) WebClient >>>>>>>>>>>> Introduced in Spring 5.X. 4) FeignClient >>>>>>>>>>>> Spring Cloud. * RestTemplate is used to develop the Rest Clients in synchronous manner that means after making Rest Call we have to wait untill we received response from Rest API then only our execution will continue. RestTemplate is an predefined Class which does not comes through Auto Configuration process... It must be created object for this class either by using "new" keyword (or) @Bean method Example ======= 1) RestTemplate rt = new RestTemplate(); (or) 2) @Bean public RestTemplate getRestTemplateObj(){ return new RestTemplate(); } RestTemplate class contains lot of utility methods such as getForEntity(),getForObject(),postForEntity(),exchange() etc., NOTE: ===== * JdbcTemplate,RestTemplate,NamedParameterJdbcTemplate,JndiTemplate etc., classes are given based template method design pattern which says that the template class takes care of common logics and programmer should take care of Specific Logics. * WebClient is modern API which is used to develop Rest Client and by using webclient we can develop both synchronous & Unsynchronous clients and we can say that Webclient is replacement of RestTemplate. * Feign Client is part of Spring Cloud Library. Steps for Developing Rest Client Program ======================================== 1) Create new Spring boot project with below dependencies 1) Spring Web 2) Spring Dev Tools 2) Develop the ApplicationConfig to mark the RestTemplate as Spring Bean ApplicationConfig.java ======================= package com.ashokit.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ApplicationConfig { //Mark the RestTemplate class as spring bean @Bean public RestTemplate getRestTemplateObject() { return new RestTemplate(); } } 3) Develop the RestClient Program in Main Class Application.java ================ package com.ashokit; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class Application implements ApplicationRunner { @Autowired private RestTemplate restTemplate; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @SuppressWarnings("deprecation") @Override public void run(ApplicationArguments args) throws Exception { //API Details :::: http://localhost:8732/customers/wishes // :::: GET Request API with out PathVariable // :::: String As Response String endPointURL = "http://localhost:8732/customers/wishes"; ResponseEntity forEntity = restTemplate.getForEntity(endPointURL, String.class); int statusCodeValue = forEntity.getStatusCodeValue(); if(statusCodeValue == 200) { System.out.println("API Response ::::" + forEntity.getBody()); //Getting Response Body } //API Details :::: http://localhost:8732/customers/wishes/{username} // :::: Path Variable name :::: username // :::: GET Request API With PathVariables // :::: String as Response endPointURL = "http://localhost:8732/customers/wishes/{username}"; ResponseEntity forEntity2 = restTemplate.getForEntity(endPointURL, String.class, Map.of("username","Mahesh")); HttpStatusCode statusCode = forEntity2.getStatusCode(); if(statusCode.is2xxSuccessful()) { String responseBody = forEntity2.getBody(); System.out.println("API Response::::::" + responseBody); } //working with getForObject() String responseBody = restTemplate.getForObject(endPointURL, String.class, Map.of("username","Ashok")); System.out.println("API Response::::" + responseBody); } } OUTPUT ====== API Response ::::Welcome To Customer Controller... API Response::::::Welcome To Customer Controller Mahesh API Response::::Welcome To Customer Controller Ashok NOTE: ===== * Make sure Producer Application should be run and UP and also verify those API's are working or not by testing through postman (or) Swagger. IIQ) Differences between getForEntity() Vs getForObject() ? -> getForEntity() return type is ResponseEntity which contains more details of response object(Response status code,Response Body,Response Headers etc.,) -> getForObject() return type is T(Typed Parameter) simply will returns the Response Body. Application.java ================ package com.ashokit; import java.time.LocalDate; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatusCode; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import com.ashokit.dto.PersonDTO; import com.fasterxml.jackson.databind.ObjectMapper; @SpringBootApplication public class Application implements ApplicationRunner { @Autowired private RestTemplate restTemplate; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @SuppressWarnings("deprecation") @Override public void run(ApplicationArguments args) throws Exception { //API Details :::: http://localhost:8732/customers/wishes // :::: GET Request API with out PathVariable // :::: String As Response String endPointURL = "http://localhost:8732/customers/wishes"; ResponseEntity forEntity = restTemplate.getForEntity(endPointURL, String.class); int statusCodeValue = forEntity.getStatusCodeValue(); if(statusCodeValue == 200) { System.out.println("API Response ::::" + forEntity.getBody()); //Getting Response Body } //API Details :::: http://localhost:8732/customers/wishes/{username} // :::: Path Variable name :::: username // :::: GET Request API With PathVariables // :::: String as Response endPointURL = "http://localhost:8732/customers/wishes/{username}"; ResponseEntity forEntity2 = restTemplate.getForEntity(endPointURL, String.class, Map.of("username","Mahesh")); HttpStatusCode statusCode = forEntity2.getStatusCode(); if(statusCode.is2xxSuccessful()) { String responseBody = forEntity2.getBody(); System.out.println("API Response::::::" + responseBody); } //working with getForObject() String responseBody = restTemplate.getForObject(endPointURL, String.class, Map.of("username","Ashok")); System.out.println("API Response::::" + responseBody); //working with exchange() endPointURL = "http://localhost:8732/customers/wishes"; ResponseEntity exchangeMessage = restTemplate.exchange(endPointURL, HttpMethod.GET, null, String.class); if(exchangeMessage.getStatusCodeValue() == 200) { String responseMessage = exchangeMessage.getBody(); System.out.println("API Response:::::" + responseMessage); } //API Details ::: http://localhost:8732/persons/ PersonDTO dto = new PersonDTO(); dto.setName("Test-1"); dto.setEmailId("test-1@ashokit.com"); dto.setContactNo("1232323333"); dto.setLocation("Hyderabad"); //dto.setCreatedDate(LocalDate.now()); //converting the Java Object into JSON ObjectMapper objectMapper = new ObjectMapper(); String jsonString = objectMapper.writeValueAsString(dto); System.out.println("JSON String::::" + jsonString); //Prepare Headers HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); //Preparing the HttpRequest using HttpEntity object with headers HttpEntity request = new HttpEntity<>(jsonString,headers); ResponseEntity postForEntity = restTemplate.postForEntity("http://localhost:8732/persons/", request, String.class); if(postForEntity.getStatusCodeValue() == 201) { String postResponse = postForEntity.getBody(); System.out.println("API Response:::::" + postResponse); } } } OUTPUT ====== API Response ::::Welcome To Customer Controller... API Response::::::Welcome To Customer Controller Mahesh API Response::::Welcome To Customer Controller Ashok API Response:::::Welcome To Customer Controller... JSON String::::{"id":null,"name":"Test-1","location":"Hyderabad","contactNo":"1232323333","emailId":"test-1@ashokit.com","createdDate":null} API Response:::::Person Registered Successfully With Person ID Has :::::13 Assignment ========== * Testing POST Request API By using exchange().........