استدعاء واجهات API أخرى من Spring
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
call an external HTTP API from your service with RestClient.
الموضوعات المخطط لها
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.
تفاصيل المخطط المبدئي
عرض عملي
RestClient client = RestClient.builder().baseUrl("https://rates.example.com").build();
Rates r = client.get().uri("/latest?base=SAR").retrieve().body(Rates.class);اختبر فهمك
Why configure timeouts?
أظهر الإجابة
A slow remote must not block your service forever.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.