国別APIの基本
Liferay 7.4 U24+ および GA24+
LiferayのREST APIを使用して、国を作成および管理します。
国を追加する
Liferay DXP の新しいインスタンスを起動するには、以下を実行します。
docker run -it -m 8g -p 8080:8080 liferay/dxp:2026.q1.9-lts
http://localhost:8080 に、メールアドレス test@liferay.com とパスワード test を使用して 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"、 "番号" : 1234、 "位置" : 0.0、 "領域" : [ ]、 "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\", \"名前\": \"Foo\"、\"number\": \"1234\"}" | お客様が掲載を希望するデータ |
-u "test@liferay.com:learn" | 基本的な認証情報 |
ここでは、デモンストレーション目的で基本認証を使用しています。 本番環境では、 OAuth2 を介してユーザーを認証する必要があります。 OAuth2 を使用する React アプリケーションの例については、 OAuth2 を使用してユーザーを認証する を参照してください。
他のcURLコマンドも同様のJSON引数を使用します。
Javaクラスを調べます
Countries_POST_ToInstance.java クラスは、 CountryResource サービスを呼び出すことで国を追加します。
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 = ... | ビルダー を取得して、 CountryResource サービス インスタンスを生成します。 |
CountryResource countryResource = builder.authentication(...).build(); | 基本認証を指定し、 CountryResource サービス インスタンスを生成します。 |
Country country = countryResource.postCountry(...); | countryResource.postCountry メソッドを呼び出し、データを post に渡します。 |
プロジェクトには、依存関係として com.liferay.headless.admin.address.client.jar ファイルが含まれていることに注意してください。 すべての REST アプリケーションのクライアント JAR 依存関係情報は、インストール先の API エクスプローラーの /o/api で確認できます。
main メソッドのコメントは、クラスを実行する方法を示しています。
他の例の Java クラスはこれに似ていますが、異なる CountryResource メソッドを呼び出します。
サービスの詳細については、 CountryResource を参照してください。
以下は、cURLとJavaを使用して他の 国 の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();
ページ<Country> page = countryResource.getCountriesPage(
null, null, Pagination.of(1, 2), null);
System.out.println(page);
}
インスタンスの 国 オブジェクトが JSON に表示されます。
国を取得する
以下のcURLまたはJavaコマンドを使用して、特定の国を取得します。
Countries_GET_FromInstance.[java|sh] を使用して、インスタンス の国 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"))));
}
国 フィールドは 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"、
"名前": "グー"、
"番号": "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エクスプローラー は、 国 のすべてのサービスとスキーマを表示し、各サービスを試すためのインターフェースを備えています。
バッチエンジンによる国別管理
Liferay DXP 2026.Q3+
Countries API は、 com.liferay.headless.admin.address.dto.v1_0.Country エンティティ クラス名を使用して、Liferay インスタンス間で国を移行するためのバッチ エクスポートとインポートをサポートしています。
各 国 レコードには、作成者と外部参照コードが含まれています。外部参照コードは、バッチ エンジンが環境間でレコードを照合するために使用する一意の識別子です。
バッチ エンジンの使用方法の詳細については、 バッチ エンジン API を使用したデータのエクスポート および バッチ エンジン API を使用したデータのインポート を参照してください。