国別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", "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クラスは、 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サービスインスタンスを生成するためのBuilderを取得します。 |
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 エクスプローラーで確認できます。
mainメソッドのコメントは、クラスの実行方法を示しています。
他の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コマンドを使用して、特定の国を取得します。
インスタンスのCountry ID を取得するには、 Countries_GET_FromInstance.[java|sh]を使用します。
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サービスとスキーマが表示され、各サービスを試用するためのインターフェースが備わっています。
バッチエンジンによる国別管理
Countries API は、 com.liferay.headless.admin.address.dto.v1_0.Countryエンティティクラス名を使用して、Liferay インスタンス間で国を移行するためのバッチエクスポートとインポートをサポートしています。
Liferay DXP 2026.Q3以降、エクスポート/インポートUIでエンティティタイプとして国も利用できるようになりました。国別エクスポートにはその国の地域が含まれ、 Countryレコードには作成者と外部参照コード(バッチエンジンが環境間でレコードを照合するために使用する一意の識別子)が含まれます。
バッチエンジンの使用方法の詳細については、「バッチエンジンAPIを使用したデータのエクスポートとインポート」を参照してください。