###############
REST Client
################
-> The application which is accessing REST API is called as REST Client
-> Rest Client app and REST Api application will communicate using HTTP as a
mediator
-> Every Programming language having support to develop REST Client
-> Using Spring Boot also we can develop REST Client Applications
1) RestTemplate
2) WebClient (introduced in spring 5.x version)
-> RestTemplate is a predefined class, which is part of 'spring-boot-starter-web'
dependency. It supports only synchronus communication.
-> WebClient is a interface which is part of 'spring-boot-starter-webflux'
dependency. It supports both synchronus & asynchronus communication.
Note: To develop REST Client we need to know REST API details
1) API URL
2) API Request Method
2) API request data structure
3) API response data structure
-> REST API team will send swagger documentation to REST Client side team. Using
swagger documentation we need to understand the api and we need to develop REST
Client logic to access REST API.
######################################
RestTemplate Example to access REST API
######################################
******************Sending HTTP GET Request using RestTemplate*****************
String apiURL = "[Link]
RestTemplate rt = new RestTemplate();
ResponseEntity<String> forEntity = [Link](apiURL,
[Link]);
String body = [Link]();
[Link](body);
******************Sending HTTP POST Request using RestTemplate*****************
@Service
public class BookClient {
public void invokeBookTicket() {
String apiUrl = "[Link]
Book book = new Book();
[Link]("Java");
[Link](345.00);
RestTemplate rt = new RestTemplate();
ResponseEntity<String> postForEntity = [Link](apiUrl, book,
[Link]);
[Link]([Link]());
}
}
*********************Sending GET Request and binding Response JSON to Binding
Obj************************
public void invokeGetBooksNew() {
String apiUrl = "[Link]
RestTemplate rt = new RestTemplate();
ResponseEntity<Book[]> forEntity = [Link](apiUrl,
Book[].class);
Book[] body = [Link]();
for(Book book : body) {
[Link](book);
}
}
}
***********************************************************************************
*******************
###############################
What is Synchronus & Asynchronus
#################################
-> Synchronus means blocking the thread until we get response
-> Asynchronus means non-blocking thread
-> RestTemplate supports only Synchronus communications
-> WebClient supports both Sync & Async communications
-> WebClient introduced in Spring 5.x version
-> WebClient is part of WebFlux starter
GET : [Link]
POST : [Link]
############################
HTTP Post Request with WebClient
#############################
public void invokeSaveBook() {
Book book = new Book();
[Link]("Angular");
[Link](450.00);
String apiUrl = "[Link]
WebClient client = [Link]();
String resp = [Link]() // HTTP POST Request
.uri(apiUrl) // Endpoint URL
.bodyValue(book) // HTTP Request Body
Data
.retrieve() // Retrieve HTTP Response
Body
.bodyToMono([Link]) //Bind Response
to string var
.block(); // Make it as Sync client
[Link](resp);
}
################################
HTTP GET Request using WebClient
#################################
public void invokeGetBooks() {
String apiUrl = "[Link]
WebClient client = [Link]();
/*
String body = [Link]() // GET Request
.uri(apiUrl) // Endpoint URL
.retrieve() // retrieve response body
.bodyToMono([Link]) // bind response data to string var
.block(); // make it sync
*/
Book[] responseData = [Link]()
.uri(apiUrl)
.retrieve()
.bodyToMono(Book[].class)
.block();
for(Book b : responseData) {
[Link](b);
}
}
###############################
Asynchronus call using Webclient
###############################
public void invokeGetBooksAsync() {
String apiUrl = "[Link]
WebClient client = [Link]();
[Link]()
.uri(apiUrl)
.retrieve()
.bodyToMono(Book[].class)
.subscribe(BookClient::respHandler); // Async
Communication
[Link]("**************Request Sent***********");
}
public static void respHandler(Book[] books) {
for(Book b : books) {
[Link](b);
}
}