Skip to content
Ameen Abozaid
Planned lessonJNR-022-L04

Calling other APIs from Spring

  • Lesson 4 of 5
  • Junior
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

  • RestClient is 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

java
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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.