You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Spring Boot, you can call external APIs using the RestTemplate (for older versions) or WebClient (a newer alternative).
RestTemplate
RestTemplate is a class provided by the Spring Framework that simplifies the process of making HTTP requests and handling responses. It abstracts away much of the boilerplate code typically associated with making HTTP calls, making it easier to interact with RESTful web services.
So here we will be calling external API, by calling our api end points. Here as an external API we will be using JSON Placeholder which provides fake data by consuming its API.
Here we will be calling the segment path /restTemplate/get which will be calling https://jsonplaceholder.typicode.com/posts/. Lets run the application. When we hit http://localhost:8080/restTemplate/get end point we can see list of post as the json response.
getForEntity() or getForObject() sends a GET request to uri https://jsonplaceholder.typicode.com/posts/ to fetch data from the server. Retrieves the response, either as a ResponseEntity or as the response body directly.
The above was an example to retrieve data or get data, now lets see an example of post request. To send POST request we need to first form the required data which will be accepted by the server, so json placeholder opens an endpoint where we can POST below kind of data on its end point https://jsonplaceholder.typicode.com/posts
{
id: 1,
title: 'foo',
body: 'bar',
userId: 1
}
So below is the implementation using RestTemplate.
@PostMapping("/restTemplate/post")
public String postJsonData(@RequestBody String requestBody) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
ResponseEntity<String> response = restTemplate.postForEntity("https://jsonplaceholder.typicode.com/posts", request, String.class);
return response.getBody();
}
When we run the application and hit the /restTemplate/post it will post the data in the https://jsonplaceholder.typicode.com/posts server.
postForEntity() or postForObject() sends a POST request to the specified URL. Submits a request body (typically JSON or form data) to the server. Returns the response, either as a ResponseEntity (with status, headers, body) or as the response body directly.
HttpHeaders represents the HTTP headers to be included in the request. Allows you to set headers like Content-Type, Authorization, and custom headers. Here we have set content type as
Alternatively we can add more content headers like below
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/json");
headers.set("Authorization", "Bearer your_token");
HttpEntity represents the entire HTTP request body, including
Headers: Metadata about the request (e.g., content type, authentication).
Body: The actual data to send (e.g., JSON, form data).
It combines headers and body into a single object for easier handling in methods like postForEntity. The HttpEntity encapsulates the requestBody and headers into one object to pass to the RestTemplate method.
Whats the difference between postForEntity() and postForObject() ? postForEntity gives you more information (headers, status), while postForObject gives only the body.
Now lets see an example of DELETE.
@DeleteMapping("/restTemplate/delete/{id}")
public String deleteJsonData(@PathVariable String id) {
String url = "https://jsonplaceholder.typicode.com/posts/"+id;
restTemplate.delete(url);
return id+" is delete";
}
Output
delete(url) sends a DELETE request without returning any response body
WebClient
WebClient is a modern, non-blocking HTTP client introduced in the Spring WebFlux module. It is designed to handle reactive and asynchronous programming models. Unlike the older RestTemplate, which operates synchronously, WebClient is fully asynchronous and leverages reactive streams, making it suitable for high-concurrency, low-latency applications