Skip to main content

What Is a Web Client?

Let’s check the Hexagonal Architecture diagram once more and focus on the Secondary Adapter (External API) (blue box lower right outside the hexagon):

hexagon

A Web Client is a Secondary Adapter

A Web Client is a tool that handles:

  • Crafting HTTP requests.
  • Parsing responses.
  • Handling errors.

There are many libraries available for this purpose, but two of the most popular in the Spring ecosystem are RestTemplate and FeignClient.

RestTemplate: Imperative Approach

RestTemplate provides a traditional, imperative way of handling HTTP requests. With this client, you explicitly create the instance, build your requests, and handle responses step by step. This approach can offer fine-grained control over the request/response process.

RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject("https://api.example.com/data", String.class);

In this snippet, the code directly invokes the GET request and processes the returned data. It’s straightforward but requires you to write the boilerplate for each request.

FeignClient: Declarative Approach

FeignClient, on the other hand, embraces a declarative style. Instead of writing all the request-handling code manually, you define an interface annotated with details about the HTTP endpoints. Spring then automatically creates an implementation of that interface at runtime. This leads to cleaner, more maintainable code—especially as the number of external calls grows.

@FeignClient(name = "exampleClient", url = "https://api.example.com")
public interface ExampleClient {
@GetMapping("/data")
String getData();
}

Here, you declare a method for the desired HTTP operation, and the underlying framework takes care of the rest. This approach can significantly reduce the amount of boilerplate code and make your service integrations more readable.