ホームページ >Java >&#&チュートリアル >Spring Boot でサードパーティ API を呼び出す方法

Spring Boot でサードパーティ API を呼び出す方法

Patricia Arquette
Patricia Arquetteオリジナル
2025-01-23 22:04:15983ブラウズ

この Spring Boot チュートリアルでは、サードパーティ API を使用し、その結果を Thymeleaf ビューに表示する方法を示します。テキストとコードを改良して、明確さと正確さを高めましょう。

改訂されたテキスト:

概要

このチュートリアルでは、サードパーティ API を Spring Boot アプリケーションに統合する方法を説明します。 https://api.sampleapis.com/coffee/hot への GET リクエストを作成し、ブラウザーに表示される Thymeleaf テンプレート内で応答データをエレガントに表示します。

前提条件

以下に関する基本的な知識があることを前提としています:

  • ジャワ
  • スプリングブーツ
  • タイムリーフ
  • 依存関係管理用の Maven (または Gradle)

開発プロセス

1.プロジェクトのセットアップ

Spring Initializr (https://www.php.cn/link/bafd1b75c5f0ceb81050a853c9faa911) を使用して、新しい Spring Boot プロジェクトを作成します。次の依存関係を含めます:

How to call third-party API in Spring Boot

ダウンロードしたアーカイブを抽出し、プロジェクトを IDE (IntelliJ IDEA など) にインポートします。

2. Coffee モデル

を作成します

API から受け取ったコーヒー データを表す POJO (Plain Old Java Object) を作成します。 これにより、データの処理が簡素化されます。

<code class="language-java">package com.myproject.apidemo;

public class Coffee {
    public String title;
    public String description;

    // Constructors, getters, and setters (omitted for brevity)
    @Override
    public String toString() {
        return "Coffee{" +
                "title='" + title + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}</code>

3. CoffeeController

を作成します

このコントローラーは API 呼び出しとデータ処理を処理します。

<code class="language-java">package com.myproject.apidemo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.core.ParameterizedTypeReference;
import java.util.List;

@Controller
public class CoffeeController {

    @GetMapping("/coffee")
    public String getCoffee(Model model) {
        String url = "https://api.sampleapis.com/coffee/hot";

        WebClient webClient = WebClient.create();

        List<Coffee> coffeeList = webClient.get()
                .uri(url)
                .retrieve()
                .bodyToMono(new ParameterizedTypeReference<List<Coffee>>() {})
                .block();


        model.addAttribute("coffeeList", coffeeList);
        return "coffee";
    }
}</code>

注: エラー処理 (例: onErrorResumeWebClient の使用) は、実稼働対応のコードに追加する必要があります。 ここでは簡単にするために block() メソッドが使用されていますが、実際のアプリケーションでのパフォーマンスを向上させるには、リアクティブ プログラミング手法に置き換える必要があります。

4. Thymeleaf ビューの作成 (coffee.html)

コーヒー データを表示するための Thymeleaf テンプレートを作成します。 このファイルを src/main/resources/templates/coffee.html.

に配置します。
<code class="language-html"><!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Coffee List</title>
</head>
<body>
    <h3>Coffee List</h3>
    <table>
        <thead>
            <tr>
                <th>Title</th>
                <th>Description</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="coffee : ${coffeeList}">
                <td th:text="${coffee.title}"></td>
                <td th:text="${coffee.description}"></td>
            </tr>
        </tbody>
    </table>
</body>
</html></code>

5.アプリケーションを実行します

Spring Boot アプリケーションを起動します。これで、http://localhost:8080/coffee (またはアプリケーションのベース URL) にあるコーヒー リストにアクセスできるようになります。

この改訂版では、Coffee モデル クラスや改善されたコード形式などの重要な詳細を含む、プロセスのより完全かつ正確な表現が提供されます。 本番環境では潜在的なエラーに必ず対処してください。

以上がSpring Boot でサードパーティ API を呼び出す方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。