Different ways to call REST APIs in Spring Boot without additional libraries

close up shot of keyboard buttons
Photo by Miguel Á. Padriñán on Pexels.com

In today’s world most modern web apps talk to each other through HTTP APIs.

REST API is a popular standard for these HTTP APIs.

So calling a REST API from your backend app is a critical functionality.

Spring Boot provides multiple abstractions to do this in a simple way.

Here are four different ways:

Using REST Template:

This is the oldest and the standard way to make REST API calls in Spring Boot.

It provides more generic methods like exchange() and execute() where you need to specify which HTTP method to invoke in addition to the other details like the URL , request etc.

And also it provides more specific methods like getForEntity() , postForEntity() etc for specific HTTP methods.

Here are few examples:

@Autowired
private RestTemplate restTemplate;
// GET request
public ResponseEntity<String> getExample(String url) {
    return restTemplate.getForEntity(url, String.class);
}
// POST request
public ResponseEntity<String> postExample(String url, Object request) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<Object> entity = new HttpEntity<>(request, headers);
    return restTemplate.postForEntity(url, entity, String.class);
}
// PUT request
public void putExample(String url, Object request) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<Object> entity = new HttpEntity<>(request, headers);
    restTemplate.put(url, entity);
}
// DELETE request
public void deleteExample(String url) {
    restTemplate.delete(url);
}

In these examples, the RestTemplate is used to send HTTP requests for different methods (GET, POST, PUT, and DELETE).

For POST and PUT requests, an HTTP entity is created with a request object and headers, and for DELETE requests, only the URL is required.

The response for GET and POST requests is returned as a ResponseEntity, which contains both the response body and the response headers.

For PUT and DELETE requests, no response is returned, as these methods are used to modify resources on the server, not to retrieve data.

Rest Template calls are synchronous .

This class is also in maintenance mode as given in Spring Docs . They suggest to use Spring WebClient instead:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html

Using Spring WebClient:

Spring WebClient is the latest library provided by Spring to make REST API calls.

It supports both synchronous and asynchronous calls and supports reactive programming.

The library is written in fluent API style and is more readable than REST Template.

Here are few examples:

@Autowired
private WebClient webClient;
// GET request
public Mono<String> getExample(String url) {
    return webClient.get().uri(url).retrieve().bodyToMono(String.class);
}
// POST request
public Mono<String> postExample(String url, Object request) {
    return webClient.post().uri(url).contentType(MediaType.APPLICATION_JSON).bodyValue(request).retrieve().bodyToMono(String.class);
}
// PUT request
public Mono<Void> putExample(String url, Object request) {
    return webClient.put().uri(url).contentType(MediaType.APPLICATION_JSON).bodyValue(request).retrieve().bodyToMono(Void.class);
}
// DELETE request
public Mono<Void> deleteExample(String url) {
    return webClient.delete().uri(url).retrieve().bodyToMono(Void.class);
}

In these examples, the WebClient is used to send HTTP requests for different methods (GET, POST, PUT, and DELETE).

For POST and PUT requests, the request body is set with the bodyValue method, and for DELETE requests, only the URL is required.

The response for GET and POST requests is returned as a Mono, which is a reactive stream that represents a single value or an error.

For PUT and DELETE requests, a Mono<Void> is returned, indicating that no response body is expected.

For detailed examples check here:

Using Spring OpenFeign:

Spring Open Feign provides an even higher level of abstraction to invoke REST APIs.

It follows a declarative approach where you just declare the APIs you need to call in an interface. This interface is annotated with @FeignClient annotation provided by Spring which will supply the background implementation at run time.

Here are few examples:

@FeignClient(name = "example", url = "http://example.com")
public interface ExampleClient {
    @RequestMapping(method = RequestMethod.GET, value = "/example")
    String getExample();
    @RequestMapping(method = RequestMethod.POST, value = "/example")
    String postExample(@RequestBody Object request);
    @RequestMapping(method = RequestMethod.PUT, value = "/example")
    void putExample(@RequestBody Object request);
    @RequestMapping(method = RequestMethod.DELETE, value = "/example")
    void deleteExample();
}
// In another class:
@Autowired
private ExampleClient exampleClient;
// GET request
public String getExample() {
    return exampleClient.getExample();
}
// POST request
public String postExample(Object request) {
    return exampleClient.postExample(request);
}
// PUT request
public void putExample(Object request) {
    exampleClient.putExample(request);
}
// DELETE request
public void deleteExample() {
    exampleClient.deleteExample();
}

In these examples, an interface ExampleClient is created with @FeignClient annotation, which is a simple HTTP client used to make HTTP requests to a remote server.

The interface defines methods for different HTTP methods (GET, POST, PUT, and DELETE) using the @RequestMapping annotation, and the actual implementation of the interface is provided by OpenFeign.

The methods in the ExampleClient interface can be called like regular Java methods, and the responses for GET and POST requests are returned as regular Java objects.

For PUT and DELETE requests, no response is returned, as these methods are used to modify resources on the server, not to retrieve data.

Here is a detailed explanation of the implementation:

Using Java 11 Http Client:

Making HTTP calls in Java was quite an arduous task until Java 11.

You had to use an external library like Apache HTTPClient to make those calls.

Thanks to HTTPClient library introduced in Java 11 , making these calls just got easier.

You may not even want to use REST Template or Spring WebClient to make REST API calls as this library is more elegant (written in fluid API style).

Here are few examples:

HttpClient client = HttpClient.newBuilder().build();
// GET request
public String getExample(String url) throws IOException, InterruptedException {
    HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create(url)).build();
    HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
    return response.body();
}
// POST request
public String postExample(String url, Object request) throws IOException, InterruptedException {
    HttpRequest httpRequest = HttpRequest.newBuilder().POST(HttpRequest.BodyPublishers.ofString(new Gson().toJson(request)))
            .header("Content-Type", "application/json").uri(URI.create(url)).build();
    HttpResponse<String> response = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
    return response.body();
}
// PUT request
public void putExample(String url, Object request) throws IOException, InterruptedException {
    HttpRequest httpRequest = HttpRequest.newBuilder().PUT(HttpRequest.BodyPublishers.ofString(new Gson().toJson(request)))
            .header("Content-Type", "application/json").uri(URI.create(url)).build();
    client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
}
// DELETE request
public void deleteExample(String url) throws IOException, InterruptedException {
    HttpRequest request = HttpRequest.newBuilder().DELETE().uri(URI.create(url)).build();
    client.send(request, HttpResponse.BodyHandlers.ofString());
}

In these examples, the HttpClient is used to send HTTP requests for different methods (GET, POST, PUT, and DELETE).

For POST and PUT requests, the request body is created using the HttpRequest.BodyPublishers.ofString method, and for DELETE requests, only the URL is required.

The response for GET and POST requests is returned as a String, which contains the response body.

For PUT and DELETE requests, no response is returned, as these methods are used to modify resources on the server, not to retrieve data. The Gson library is used to convert the request body to a JSON string.

Here is a detailed explanation of HTTP Client :

That’s it!


Posted

in

by

Comments

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading