Chapter 11: Consuming REST endpoints
This introduction explains why a Spring backend may need to call another service’s REST endpoints and why developers need to understand the available approaches.
After this chapter you will be able to
- Backend Services as REST Clients. Explain how one backend service can call another service’s REST endpoint to exchange data, using the delivery, invoice, or warehouse example.
- REST client suitability. Distinguish the situations in which RestClient, OpenFeign, and WebClient are useful, without describing how to implement calls with them.
- Payment endpoint exchange. Trace the example payment request and response used to set up the later client demonstrations.
- Payment request path. Trace how the example app handles a call to its /payment endpoint: its controller delegates to a proxy, the proxy uses RestClient to call the other service, and the app returns that service’s response.
- OpenFeign client contract. Explain how an annotated interface defines an OpenFeign client without a hand-written implementation. Describe how @FeignClient identifies the client and its base URI, while method annotations specify each endpoint call.
- Client discovery and injection. Trace how a declared OpenFeign client becomes available to a controller through configuration and injection.
- WebClient Selection Criteria. Explain why WebClient is the appropriate REST client for a reactive app, while RestClient or OpenFeign is preferable when the app is not reactive.
- Thread Bound Versus Reactive Flows. Contrast a nonreactive request flow with a reactive one using the debt-calculation example, including what happens during I/O waits and to tasks that do not depend on each other.
- Producer Subscriber Task Dependencies. Explain how producers and subscribers express dependencies between tasks in the reactive flow.
- WebClient Proxy Configuration. Describe the setup and call details given for the WebClient proxy that sends a POST request to the payment service.
- Mono Input And Output. Trace how Mono connects the request-body task, the WebClient call, and the functionality that subscribes to the proxy’s result.
What it builds on
- Using REST services to exchange data between apps (chapter 10) · REST services exchange data between apps.
- Using a request body to get data from the client (chapter 10) · Assumes familiarity with an endpoint receiving data in a request body.
- Setting the response status and headers (chapter 10) · Assumes familiarity with headers in an HTTP response.
- Implementing REST endpoints (chapter 10) · The idea of an app exposing a REST endpoint.
- Using dependency injection with abstractions (chapter 4) · Injecting Spring beans through abstractions
Section by section
Introduction
This introduction explains why a Spring backend may need to call another service’s REST endpoints and why developers need to understand the available approaches.
- Backend Services as REST Clients: Explain how one backend service can call another service’s REST endpoint to exchange data, using the delivery, invoice, or warehouse example.
Why it matters: It explains why a Spring service needs to make REST calls, not just expose endpoints. - Value of Understanding REST Calls: Justify why developers need to understand approaches to calling REST endpoints, even when an AI tool can generate the code.
Why it matters: Understanding the approaches helps you evaluate generated code rather than relying on it blindly.
Key terms: REST services, REST endpoint, backend component, REST endpoints, available approaches
11.1 What do you need to know?
This section compares when to use three Spring REST clients and introduces the payment endpoint used to demonstrate them.
- REST client suitability: Distinguish the situations in which RestClient, OpenFeign, and WebClient are useful, without describing how to implement calls with them.
Why it matters: The choice of client depends on the kind of service call the application needs. - Payment endpoint exchange: Trace the example payment request and response used to set up the later client demonstrations.
Why it matters: The same request and response give each later client example a concrete endpoint to call.
Key terms: RestClient, OpenFeign, WebClient, non-blocking calls, payment service, Payment, HTTP POST, request header, request body, response header
11.2 Calling REST endpoints using RestClient
This section shows how a Spring app uses RestClient to call another app’s REST endpoint and return its response.
- Payment request path: Trace how the example app handles a call to its /payment endpoint: its controller delegates to a proxy, the proxy uses RestClient to call the other service, and the app returns that service’s response.
Why it matters: It shows which part of the app calls the other service and how the caller receives the result. - RestClient choice and availability: Explain why the section presents RestClient as a simple choice for calling REST endpoints and whether this Spring WebMvc app needs another dependency to use it.
Why it matters: It explains why the example can make an outgoing REST call without adding another dependency. - Two-app test setup: Describe why the example changes Tomcat’s port and what a caller observes when testing the two running apps.
Why it matters: The separate ports let a reader run both apps and see the response pass through the calling app.
Key terms: RestClient, proxy, controller, /payment endpoint, Spring WebMvc dependency, application.properties, Tomcat, port 9090, port 8080
11.3 Calling REST endpoints using Spring Cloud OpenFeign
The section shows how to consume a REST endpoint through an annotated OpenFeign interface and contrasts it with a legacy RestTemplate call.
- OpenFeign client contract: Explain how an annotated interface defines an OpenFeign client without a hand-written implementation. Describe how @FeignClient identifies the client and its base URI, while method annotations specify each endpoint call.
Why it matters: You can describe an outgoing REST call with a contract instead of writing its calling code. - Client discovery and injection: Trace how a declared OpenFeign client becomes available to a controller through configuration and injection.
Why it matters: Declaring the interface is not enough; the application must enable client discovery before it can inject and use the client. - Environment specific client URIs: Explain where to keep a client URI that changes between environments and how to refer to it from source code.
Why it matters: Changing a service address for another environment should not require recompiling the app. - RestTemplate legacy status: Explain why the section treats RestTemplate as a legacy approach rather than recommending it for new apps.
Why it matters: It helps you recognize RestTemplate in older projects without mistaking it for the preferred choice in a new one. - RestTemplate request construction: Trace the steps the example uses to send a payment request with RestTemplate.
Why it matters: This sequence lets you follow a RestTemplate call when you encounter one in an older application.
Key terms: OpenFeign client, proxy interface, @FeignClient, name, url, @PostMapping, @EnableFeignClients, client contracts, bean, inject
11.4 Calling REST endpoints using WebClient
This section explains when WebClient fits a Spring app, how reactive tasks differ from a thread-bound flow, and how to use WebClient to call a REST endpoint.
- WebClient Selection Criteria: Explain why WebClient is the appropriate REST client for a reactive app, while RestClient or OpenFeign is preferable when the app is not reactive.
Why it matters: The client should fit the app’s design rather than be chosen simply because it is recommended. - Thread Bound Versus Reactive Flows: Contrast a nonreactive request flow with a reactive one using the debt-calculation example, including what happens during I/O waits and to tasks that do not depend on each other.
Why it matters: This contrast explains the resource use and potential performance benefit motivating the reactive approach. - Producer Subscriber Task Dependencies: Explain how producers and subscribers express dependencies between tasks in the reactive flow.
Why it matters: Producer–subscriber links are the section’s explanation of how a reactive flow is assembled. - WebClient Proxy Configuration: Describe the setup and call details given for the WebClient proxy that sends a POST request to the payment service.
Why it matters: These are the concrete pieces the section gives for making the example REST call with WebClient. - Mono Input And Output: Trace how Mono connects the request-body task, the WebClient call, and the functionality that subscribes to the proxy’s result.
Why it matters: The Mono input and output show how this WebClient call differs from passing and returning values directly. - AI Proxy Generation Context: Describe what to give an AI agent when asking it to generate a REST client that matches the application and endpoint.
Why it matters: Clear technology, codebase, and API context reduces inconsistent clients and incorrect assumptions.
Key terms: WebClient, reactive app, RestClient, OpenFeign, nonreactive app, thread, I/O call, tasks, producer, subscriber
11.5 Questions
This section lists questions for reviewing the chapter’s REST client concepts, with answers in Appendix A.
11.6 Summary
The section summarizes why backend apps call other apps’ REST endpoints and how to choose among Spring’s client options.
Concepts explained in this chapter
- WebClientWebClient is a non-blocking, reactive HTTP client API in Spring for making web requests and processing responses.
- OpenFeignOpenFeign is a declarative HTTP client library that generates implementations of Java interfaces for calling remote services.
- reactive programmingA reactive approach structures asynchronous operations around nonblocking results and declared dependencies.
- MonoMono is a Reactor publisher type that represents an asynchronous computation producing zero or one result.
- reactive applicationA reactive application coordinates asynchronous data flows and tasks without blocking threads during I/O.
- sink nodeA subscriber is a reactive component that attaches to a producer and consumes its eventual result.
- threadA thread is an independently schedulable execution unit that runs instructions within an application process.
- RestTemplateRestTemplate is a Spring HTTP client API for synchronously sending requests and receiving responses from remote services.
- RestClientRestClient is Spring's synchronous HTTP client for sending requests and waiting for responses from remote services.
- @FeignClient annotationThe @FeignClient annotation declares an interface as a client for a remote service and provides configuration for generating its implementation.
- @EnableFeignClients annotationThe @EnableFeignClients annotation enables OpenFeign client support and configures scanning for client interfaces.
- OpenFeign clientAn OpenFeign client is an annotated Java interface whose implementation is generated to call remote HTTP endpoints.
- interfaceA Java interface is a reference type that declares a responsibility or contract whose behavior must be provided by implementing classes.
- proxyA proxy object intercepts calls to a target object and can apply additional behavior before delegating to it.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
