サンプルAPIアプリケーションの構築

Liferay DXP 2023.Q4+/Portal 7.4 GA102+ Beta Feature

important

APIビルダーはベータ機能で、現在はLiferayオブジェクトのみをサポートしています。

以下の手順に従って、モックAPIアプリケーションの例をご覧ください。

APIビルダーを有効にする、

  1. グローバル・メニューに移動する (グローバル・メニュー) → コントロールパネルインスタンス設定 .

  2. 「プラットフォーム」の下にある「機能フラグ」をクリックします。

  3. 左のナビゲーションで Beta を選択。 APIビルダーをオンにする。 APIビルダーが有効になりました。

サンプルオブジェクトとエントリーの追加

新しいLiferay DXPインスタンスを起動し、以下を実行します。

docker run -it -m 8g -p 8080:8080 liferay/dxp:2024.q1.1。

メールアドレス test@liferay.com とパスワード test を使用して、http://localhost:8080でLiferayにサインインしてください。 プロンプトが表示されたら、パスワードを learn に変更します。

次に、以下の手順に従います。

  1. コマンドラインで以下のスクリプトを実行し、Studentオブジェクトを作成する。

    student=$(curl \
    	-H "Content-Type: application/json" \
    	-X POST \
    	"http://localhost:8080/o/object-admin/v1.0/object-definitions" \
    	-d "{\"label\": {\"en_US\": \"Student\"}, \"name\": \"Student\", \"panelCategoryKey\": \"control_panel.object\", \"pluralLabel\": {\"en_US\": \"Students\"}, \"scope\": \"company\"}" \
      -u "test@liferay.com:learn" | jq -r ".id")
    
    curl \
    	-H "Content-Type: application/json" \
    	-X POST \
    	"http://localhost:8080/o/object-admin/v1.0/object-definitions/${student}/object-fields" \
    	-d "{\"businessType\": \"Text\", \"label\": {\"en_US\": \"First Name\"}, \"name\": \"firstName\", \"required\": false}" \
    	-u "test@liferay.com:learn"
    
    curl \
    	-H "Content-Type: application/json" \
    	-X POST \
    	"http://localhost:8080/o/object-admin/v1.0/object-definitions/${student}/object-fields" \
    	-d "{\"businessType\": \"Text\", \"label\": {\"en_US\": \"Last Name\"}, \"name\": \"lastName\", \"required\": false}" \
    	-u "test@liferay.com:learn"
    
    curl \
    	-H "Content-Type: application/json" \
    	-X POST \
    	"http://localhost:8080/o/object-admin/v1.0/object-definitions/${student}/publish" \
    	-u "test@liferay.com:learn"
    
  2. グローバルメニューに移動する (グローバルメニュー) → コントロールパネルオブジェクト . 新しい Student オブジェクトが作成されたことを確認する。

  3. コマンドラインで以下のスクリプトを実行し、Courseオブジェクトを作成する。

    course=$(curl \
    	-H "Content-Type: application/json" \
    	-X POST \
    	"http://localhost:8080/o/object-admin/v1.0/object-definitions" \
    	-d "{\"label\": {\"en_US\": \"Course\"}, \"name\": \"Course\", \"panelCategoryKey\": \"control_panel.object\", \"pluralLabel\": {\"en_US\": \"Courses\"}, \"scope\": \"company\"}" \
    	-u "test@liferay.com:learn" | jq -r ".id")
    
    curl \
    	-H "Content-Type: application/json" \
    	-X POST \
    	"http://localhost:8080/o/object-admin/v1.0/object-definitions/${course}/object-fields" \
    	-d "{\"businessType\": \"Text\", \"label\": {\"en_US\": \"Course Name\"}, \"name\": \"courseName\", \"required\": false}" \
      -u "test@liferay.com:learn"
    
  4. グローバルメニューに移動する (グローバルメニュー) → コントロールパネルオブジェクト . 先ほど作成したCourseオブジェクトをクリックしてください。

  5. 「関係」タブをクリックする。 追加]をクリックします(アイコンの追加)。

  6. 以下の値を入力して、新しいリレーションシップを定義する。

    項目
    ラベル 入学
    名前 在籍
    種類 1 対多
    次の 1 レコード: コース
    多くの記録 学生

    「保存」をクリックする。

  7. 「詳細」タブをクリックする。 Publish をクリックする。

サンプルコースと学生の追加

  1. コマンドラインで以下のスクリプトを実行し、2つのコースと2人の学生を作成します。

    math101=$(curl \
    	-H "Content-Type: application/json" \
    	-X POST \
    	"http://localhost:8080/o/c/courses" \
    	-d "{\"courseName\": \"Math 101\"}" \
    	-u "test@liferay.com:learn" | jq -r ".id")
    
    curl \
    	-H "Content-Type: application/json" \
    	-X POST \
    	"http://localhost:8080/o/c/students" \
    	-d "{\"r_enrollment_c_courseId\":${math101}, \"firstName\": \"Mary\", \"lastName\": \"Liferay\"}" \
    	-u "test@liferay.com:learn"
    
    history101=$(curl \
    	-H "Content-Type: application/json" \
    	-X POST \
      "http://localhost:8080/o/c/courses" \
    	-d "{\"courseName\": \"History 101\"}" \
    	-u "test@liferay.com:learn" | jq -r ".id")
    
    curl \
    	-H "Content-Type: application/json" \
    	-X POST \
    	"http://localhost:8080/o/c/students" \
    	-d "{\"r_enrollment_c_courseId\":${history101}, \"firstName\": \"John\", \"lastName\": \"Liferay\"}" \
    	-u "test@liferay.com:learn"
    

APIアプリケーションの作成

  1. グローバルメニューに移動する (グローバルメニュー) → コントロールパネルAPIビルダー .

  2. [追加]をクリックします(アイコンの追加)。 新しいAPIアプリケーションのタイトル、URL、説明を入力します。

    Create a new API application

    項目
    タイトル 登録
    URL 登録
    説明 学生登録を取得するAPI。

    「作成」をクリックする。

  3. 「スキーマ」タブをクリックする。 追加]をクリックします(アイコンの追加)。

  4. 新しいスキーマの名前、説明、オブジェクトを入力します。

    項目
    名前 コース登録
    説明 コースと受講生のリスト。
    オブジェクト コース

    「作成」をクリックする。

  5. 新しく作成したスキーマの Properties タブをクリックします。 左メニューの「コース名」をクリックして、「コース名」フィールドを追加してください。

    Add the course name field to the schema.

  6. 左のメニューで View Related Objects をクリックする。 Student をクリックして属性を展開します。 左メニューの First Name Last Name をクリックして、First NameLast Nameフィールドを追加する。 右上の Save_をクリックします。

  7. 次に、 Endpoints タブをクリックする。 追加]をクリックします(アイコンの追加)。

  8. 新しいAPIエンドポイントのスコープ、パス、説明を入力します。

    | 項目 | 値 | | :- | :———————— | | 範囲 | 会社名 | | パス | コース | | 説明 | コースとその学生のリストを取得するエンドポイント。 |

    「作成」をクリックする。

  9. 新しく作成したAPIエンドポイントの Configuration タブをクリックします。 レスポンス本文スキーマで、前のステップで作成した Course Registrations スキーマを選択してください。 ページ右上の Publish をクリックする。

  10. APIエクスプローラー(例: http://localhost:8080/o/api?endpoint=http://localhost:8080/o/c/registration/openapi.jsonに移動して、新しいAPIアプリケーションをテストする。

GET courses エンドポイントをクリックし、 Execute をクリックします。

Execute the Get courses endpoint.

カスタムAPIアプリケーションは、コースとコースを受講している学生のリストを返します。

Ask

Capabilities

Product

DXP

Contact Us

Connect

Powered by Liferay
© 2024 Liferay Inc. All Rights Reserved • Privacy Policy