国別APIの基礎
Liferay 7.4 U24+ および GA24+
Liferay の Rest API を使用して国を作成および管理します。
国を追加する
新しいLiferay DXPインスタンスを起動し、以下を実行します。
docker run -it -m 8g -p 8080:8080 liferay/dxp:2025.q1.6-lts
メールアドレス test@liferay.com とパスワード testを使用して、 http://localhost:8080 で Liferay にサインインします。 プロンプトが表示されたら、パスワードを learnに変更します。
次に、以下の手順に従います。
-
Countries API Basicsをダウンロードして解凍します。
curl https://resources.learn.liferay.com/examples/liferay-g6m8.zip -Ounzip liferay-g6m8.zip -
cURL スクリプトを使用して、インスタンスに新しい国を追加します。 コマンドラインで、
curlフォルダに移動します。Countries_POST_ToInstance.shスクリプトを実行します。./Countries_POST_ToInstance.shJSON 応答には、新しい国が追加されたことが示されています。
{ "a2" : "AB", "a3" : "ABL", "active" : true, "billingAllowed" : true, "groupFilterEnabled" : false, "id" : 43501, "name" : "Foo", "number" : 1234, "position" : 0.0, "regions" : [ ], "shippingAllowed" : true, "subjectToVAT" : false, "title_i18n" : { }, "zipRequired" : true } -
RESTサービスは、Javaクライアントを使って呼び出すこともできます。
curlフォルダから、javaフォルダに移動します。 ソースファイルを次のようにコンパイルします。javac -classpath .:* *.java -
Countries_POST_ToInstance.javaクラスを実行します。java -classpath .:* Countries_POST_ToInstance
cURLコマンドの検証
Countries_POST_ToInstance.sh スクリプトは、cURL コマンドを使用して REST サービスを呼び出します。
curl \
"http://localhost:8080/o/headless-admin-address/v1.0/countries" \
--data-raw '
{
"a2": "AB",
"a3": "ABL",
"name": "Foo",
"number": "1234"
}' \
--header "Content-Type: application/json" \
--request "POST" \
--user "test@liferay.com:learn"
ここでは、コマンドの引数を紹介します。
| 引数 | 説明 |
|---|---|
-H "Content-Type: application/json" | リクエストボディのフォーマットがJSONであることを示します。 |
-X POST | 指定されたエンドポイントで起動するHTTPメソッド |
"http://localhost:8080/o/headless-admin-address/v1.0/countries" | RESTサービスのエンドポイント |
-d "{\"a2\": \"AB\", \"a3\": \"ABL\", \"name\": \"Foo\", \"number\": \"1234\"}" | お客様が掲載を希望するデータ |
-u "test@liferay.com:learn" | 基本的な認証情報 |
ここでは、デモのために基本的な認証を使用しています。 本番環境では、 OAuth2経由でユーザーを認証する必要があります。 OAuth2 を使用するサンプル React アプリケーションについては、 OAuth2 を使用してユーザーを承認する を参照してください。
他のcURLコマンドも同様のJSON引数を使用しています。
Javaクラスを調べる
Countries_POST_ToInstance.java クラスは、Country 関連のサービスを呼び出して国を追加します。
public static void main(String[] args) throws Exception {
CountryResource.Builder builder = CountryResource.builder();
CountryResource countryResource = builder.authentication(
"test@liferay.com", "learn"
).build();
Country country = countryResource.postCountry(
new Country() {
{
a2 = "AB";
a3 = "ABL";
name = "Foo";
number = 1234;
}
});
System.out.println(country);
}
このクラスは、わずか3行のコードでRESTサービスを呼び出します。
| 行(省略形) | 説明 |
|---|---|
CountryResource.Builder builder = ... | Builder を取得して、 CountryResource サービス インスタンスを生成します。 |
CountryResource countryResource = builder.authentication(...).build(); | 基本認証を指定し、 CountryResource サービス インスタンスを生成します。 |
Country country = countryResource.postCountry(...); | countryResource.postCountry メソッドを呼び出し、データを post に渡します。 |
プロジェクトには、依存関係としてcom.liferay.headless.admin.address.client.jarファイルが含まれていることに注意してください。 すべてのRESTアプリケーションのクライアントJAR依存関係情報は、/o/apiでインストール先のAPIエクスプローラーで確認できます。
メイン メソッドのコメントは、クラスの実行を示しています。
他の Java クラスの例もこれに似ていますが、異なる CountryResource メソッドを呼び出します。
サービスの詳細については、 CountryResource を参照してください。
以下は、cURL と Java を使用して他の Country REST サービスを呼び出す例です。
インスタンスから国を取得する
次の cURL または Java コマンドを実行すると、国を一覧表示できます。
Countries_GET_FromInstance.sh
コマンド:
./Countries_GET_FromInstance.sh
コード:
curl \
"http://localhost:8080/o/headless-admin-address/v1.0/countries" \
--user "test@liferay.com:learn"
Countries_GET_FromInstance.java
コマンド:
java -classpath .:* Countries_GET_FromInstance
コード:
public static void main(String[] args) throws Exception {
CountryResource.Builder builder = CountryResource.builder();
CountryResource countryResource = builder.authentication(
"test@liferay.com", "learn"
).build();
Page<Country> page = countryResource.getCountriesPage(
null, null, Pagination.of(1, 2), null);
System.out.println(page);
}
インスタンスの Country オブジェクトが JSON に表示されます。
国を取得する
次の cURL または Java コマンドを使用して特定の国を取得します。
Countries_GET_FromInstance.[java|sh] を使用して、インスタンス Country ID を取得します。
Countries_GET_ById.sh
コマンド:
./Countries_GET_ById.sh 1234
コード:
curl \
"http://localhost:8080/o/headless-admin-address/v1.0/countries/${1}" \
--user "test@liferay.com:learn"
Countries_GET_ById.java
コマンド:
java -classpath .:* -DcountryId=1234 Countries_GET_ById
コード:
public static void main(String[] args) throws Exception {
CountryResource.Builder builder = CountryResource.builder();
CountryResource countryResource = builder.authentication(
"test@liferay.com", "learn"
).build();
System.out.println(
countryResource.getCountry(
Long.valueOf(System.getProperty("countryId"))));
}
Country フィールドが JSON に表示されます。
国をパッチする
次の cURL および Java コマンドを使用して、既存の国を部分的に編集します。 1234 をあなたの国のIDに置き換えてください。
Countries_PATCH_ById.sh
コマンド:
./Countries_PATCH_ById.sh 1234
コード:
curl \
"http://localhost:8080/o/headless-admin-address/v1.0/countries/${1}" \
--data-raw '
{
"name": "Bar"
}' \
--header "Content-Type: application/json" \
--request "PATCH" \
--user "test@liferay.com:learn"
Countries_PATCH_ById.java
コマンド:
java -classpath .:* -DcountryId=1234 Countries_PATCH_ById
コード:
public static void main(String[] args) throws Exception {
CountryResource.Builder builder = CountryResource.builder();
CountryResource countryResource = builder.authentication(
"test@liferay.com", "learn"
).build();
Country country = countryResource.patchCountry(
Long.valueOf(System.getProperty("countryId")),
new Country() {
{
name = "Bar";
}
});
System.out.println(country);
}
国を置く
次の cURL および Java コマンドを使用して、既存の国を完全に上書きします。 1234 をあなたの国のIDに置き換えてください。
Countries_PUT_ById.sh
コマンド:
./Countries_PUT_ById.sh 1234
コード:
curl \
"http://localhost:8080/o/headless-admin-address/v1.0/countries/${1}" \
--data-raw '
{
"a2": "AB",
"a3": "ABL",
"name": "Goo",
"number": "1234"
}' \
--header "Content-Type: application/json" \
--request "PUT" \
--user "test@liferay.com:learn"
Countries_PUT_ById.java
コマンド:
java -classpath .:* -DcountryId=1234 Countries_PUT_ById
コード:
public static void main(String[] args) throws Exception {
CountryResource.Builder builder = CountryResource.builder();
CountryResource countryResource = builder.authentication(
"test@liferay.com", "learn"
).build();
Country country = countryResource.putCountry(
Long.valueOf(System.getProperty("countryId")),
new Country() {
{
a2 = "AB";
a3 = "ABL";
name = "Goo";
number = 1234;
}
});
System.out.println(country);
}
国を削除する
次の cURL および Java コマンドを使用して、既存の国を削除します。 1234 をあなたの国のIDに置き換えてください。
Countries_DELETE_ById.sh
コマンド:
./Countries_DELETE_ById.sh 1234
コード:
curl \
"http://localhost:8080/o/headless-admin-address/v1.0/countries/${1}" \
--request "DELETE" \
--user "test@liferay.com:learn"
Countries_DELETE_ById.java
コマンド
java -classpath .:* -DcountryId=1234 Countries_DELETE_ById
コード:
public static void main(String[] args) throws Exception {
CountryResource.Builder builder = CountryResource.builder();
CountryResource countryResource = builder.authentication(
"test@liferay.com", "learn"
).build();
countryResource.deleteCountry(
Long.valueOf(System.getProperty("countryId")));
}
API エクスプローラー には、すべての Country サービスとスキーマが表示され、各サービスを試すためのインターフェースがあります。