Overview

11 Consuming REST endpoints

Modern backend systems often need one service to call another service’s REST endpoint, and this chapter explains the main Spring options for doing that clearly and correctly. It uses a simple payments example to show how a backend app can send a request with headers and a body, receive the response, and pass it back to the original caller. The goal is not just to make the call work, but to understand which approach fits a given application and why.

The chapter first presents a straightforward solution with RestClient, which is favored for its simple, readable API and works well for common synchronous calls in regular Spring web applications. It then introduces OpenFeign, which reduces boilerplate by letting you define a REST contract as an interface and having Spring generate the implementation. The discussion also contrasts this with the older RestTemplate, showing why it is now considered legacy and less suitable for newer applications.

Finally, the chapter explains WebClient and the reactive style behind it, emphasizing that it is best suited for reactive applications rather than ordinary blocking ones. It shows how reactive programming treats work as connected tasks rather than one thread doing everything in sequence, which can improve efficiency when multiple I/O calls are involved. Across all three approaches, the chapter reinforces good practices such as keeping URLs in properties, using the right client for the architecture, and giving AI tools enough context to generate consistent REST client code.

Often, a backend app needs to act as a client for another backend app and calls exposed REST endpoints to work with specific data.
A client sends a request to the backend app, which in turn needs to call another backend service. The backend app will use clients such as RestClient or OpenFeign to properly send the HTTP requests to the other backend component.
The payment service exposes an endpoint that requires an HTTP request body. The app uses RestClient, OpenFeign, or WebClient to send requests to the endpoint that the payment service exposes.
A backend app needs to send HTTP requests to another backend service. We use RestClient to implement the functionality that consumes the REST endpoint.
With OpenFeign, you only need to define an interface (a contract) and tell OpenFeign where to find this contract to implement it. OpenFeign implements the interface and provides the implementation as a bean in the Spring context based on configurations you define with annotations. You can inject the bean from the Spring context anywhere you need it in your app.
A functionality scenario for demonstrating the usefulness of a reactive approach. A banking app needs to call several other apps to calculate the total debt of a user. Due to these calls, the thread executing the request is blocked several times while waiting for I/O operations to finish.
The execution of the scenario functionality from the thread point of view. The arrow represents the timeline of the thread. Some of the steps cause details to block the thread, which needs to wait for the task to finish before proceeding.
An analogy of the way a reactive app works. A thread doesn’t take a request’s tasks in order and wait when it’s blocked. Instead, all tasks from all requests are on a backlog. Any available thread can work on tasks from any request. This way, independent tasks can be solved in parallel, and the threads don’t stay idle.
In a reactive app, the steps become tasks. Each task marks its dependencies on other tasks and allows other tasks to depend on them. Threads are free to execute any task.
The tasks chain in a reactive app. When building a reactive web app, we define the tasks and the dependencies between them. The WebFlux functionality, initiating the HTTP request, subscribes to the task we create through the producer the controller’s action returns. In our case, this producer is the one we get by sending the HTTP request with WebClient. For WebClient to make the request, it subscribes to another task that provides the request body.

Summary

  • In a real-world backend solution, you often find cases when a backend app needs to call endpoints exposed by another backend app.
  • Spring offers multiple solutions for implementing the client side of a REST service. Three of the most relevant solutions are as follows:
    • RestClient—A simple tool used to call REST endpoints in Spring apps
    • OpenFeign—A solution offered by the Spring Cloud project that successfully simplifies the code you need to write to call a REST endpoint and adds several features relevant to how we implement services today
    • WebClient—A reactive solution for calling REST endpoints in a Spring app
  • You shouldn’t use RestTemplate in new implementations. You can choose between RestClient or OpenFeign for non-reactive apps, and WebClient for reactive apps to call REST endpoints.
  • For an app following a standard (nonreactive) approach, the best choice is using RestClient or OpenFeign.
  • WebClient is an excellent tool for an app designed on a reactive approach. But before using it, you should deeply understand the reactive approach and how to implement a reactive app with Spring.

FAQ

What is RestClient?RestClient is a modern Spring client for sending HTTP requests and reading HTTP responses in a simple, direct, and readable way. It is a good choice for common synchronous REST calls.
What is the role of a proxy class when consuming a REST endpoint?A proxy class acts as the component that hides the details of the HTTP call and delegates requests to another service. It prepares the request, sends it, and returns the response to the caller.
What is OpenFeign?OpenFeign is a Spring Cloud tool that lets you define REST calls through Java interfaces. Spring creates the implementation for you, so you write less code.
What is the purpose of the @FeignClient annotation?The @FeignClient annotation marks an interface as an OpenFeign client and tells Spring to generate its implementation. It also lets you define the client name and base URL.
Why should endpoint URLs be stored in properties files?Storing endpoint URLs in properties files makes the application easier to configure for different environments. It avoids hardcoding values in the source code and prevents recompiling when the URL changes.
What is RestTemplate?RestTemplate is the older Spring approach for calling REST endpoints. It works, but it is considered legacy and is not the preferred choice for new applications.
What is WebClient?WebClient is Spring’s REST client for reactive, non-blocking communication. It is designed for reactive applications and works with publishers such as Mono.
When should you choose WebClient instead of RestClient or OpenFeign?You should choose WebClient when you are building a reactive application or when non-blocking communication is important. For standard blocking calls, RestClient or OpenFeign is usually a better fit.
How can an API contract help when generating a REST client?An API contract gives the exact endpoint details, such as path, HTTP method, request body, headers, and response type. This helps AI or developers generate a correct client that matches the service definition.
What is the main difference between RestClient and OpenFeign?RestClient is a direct HTTP client where you write the call logic yourself, while OpenFeign lets you declare the call in an interface and have Spring generate the implementation automatically.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Spring Start Here, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Spring Start Here, Second Edition ebook for free