Calling other APIs from Spring
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
call an external HTTP API from your service with RestClient.
Planned topics
RestClientis Spring's modern synchronous HTTP client (Spring 6.1+; verify your version).- Build it once as a bean with a base URL, and inject it where needed.
- Always set timeouts (connect and read); without them a slow partner can freeze your threads.
- Convert the JSON response into a record with
.body(MyRecord.class). - Handle failures: the remote may return
4xx,5xx, or time out; decide what your API returns then.
Draft outline details
Demonstration
RestClient client = RestClient.builder().baseUrl("https://rates.example.com").build();
Rates r = client.get().uri("/latest?base=SAR").retrieve().body(Rates.class);Check your understanding
Why configure timeouts?
Show answer
A slow remote must not block your service forever.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.