Countries API の基本¶
Liferay 7.4 U24以降とGA24以降で利用可能
Liferay の Rest API を使用して、国を作成および管理します。
国を追加する¶
新しいLiferay DXPインスタンスを起動し、以下を実行します。
docker run -it -m 8g -p 8080:8080 liferay/dxp:7.4.13-u55。
メールアドレス_test@liferay.com_とパスワード_test_を使用して、http://localhost:8080でLiferayにサインインしてください。 プロンプトが表示されたら、パスワードを _learn_に変更します。
次に、以下の手順を実行します。
Countries API Basics をダウンロードし、解凍してください。
curl https://learn.liferay.com/dxp/latest/ja/users-and-permissions/developer-guide/liferay-g6m8.zip -O
unzip liferay-g6m8.zip
cURLスクリプトを使用して、インスタンスに新しい国を追加します。 コマンドラインで、
curl
フォルダに移動します。Country_POST_ToInstance.sh
スクリプトを実行します。./Country_POST_ToInstance.sh
JSONレスポンスに、新しい国が追加されたことが示されています。
"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
Country_POST_ToInstance.java
クラスを実行します。java -classpath .:* Country_POST_ToInstance
cURLコマンドの検証¶
Country_POST_ToInstance.sh
スクリプトは、cURL コマンドで REST サービスを呼び出します。
curl \
-H "Content-Type: application/json" \
-X POST \
"http://localhost:8080/o/headless-admin-address/v1.0/countries" \
-d "{\"a2\": \"AB\", \"a3\": \"ABL\", \"name\": \"Foo\", \"number\": \"1234\"}" \
-u "[email protected]:learn"
ここでは、コマンドの引数を紹介します。
引数 |
説明 |
---|---|
|
リクエストボディのフォーマットがJSONであることを示します。 |
|
指定されたエンドポイントで起動するHTTPメソッド |
|
RESTサービスのエンドポイント |
|
お客様が掲載を希望するデータ |
|
基本的な認証情報 |
注釈
ここでは、デモのためにベーシック認証を使用しています。 本番環境では、 OAuth2 を使ってユーザーを認証する必要があります。 OAuth2を利用したReactアプリケーションのサンプルは、OAuth2を使ってユーザーを認証するをご参照ください。
他のcURLコマンドも同様のJSON引数を使用しています。
Javaクラスを調べる¶
Country_POST_ToInstance.java
クラスは、Country 関連サービスを呼び出すことで国を追加します。
public static void main(String[] args) throws Exception {
CountryResource.Builder builder = CountryResource.builder();
CountryResource countryResource = builder.authentication(
"[email protected]", "learn"
).build();
Country country = countryResource.postCountry(
new Country() {
{
a2 = "AB";
a3 = "ABL";
name = "Foo";
number = 1234;
}
});
System.out.println(country);
}
このクラスは、次の3行のコードのみを使用してRESTサービスを呼び出します。
行(省略形) |
説明 |
---|---|
|
|
|
Basic 認証を指定し、 |
|
|
プロジェクトには、依存関係としてcom.liferay.headless.admin.address.client.jar
ファイルが含まれていることに注意してください。 すべてのRESTアプリケーションのクライアントJAR依存関係情報は、/o/api
でインストール先の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" \
-u "[email protected]: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(
"[email protected]", "learn"
).build();
Page<Country> page = countryResource.getCountriesPage(
null, null, Pagination.of(1, 2), null);
System.out.println(page);
}
Instance の Country
オブジェクトが JSON で表示されます。
国を取得する¶
以下のcURLまたはJavaコマンドで、特定の国を取得します。
Tip
インスタンスの Countries
ID を取得するには、 Countries_GET_FromInstance.[java|sh]
を使用します。
Country_GET_ById.sh¶
コマンド:
./Country_GET_ById.sh 1234
コード:
curl \
"http://localhost:8080/o/headless-admin-address/v1.0/countries/${1}" \
-u "[email protected]:learn"
Country_GET_ById.java¶
コマンド:
java -classpath .:* -DcountryId=1234 Country_GET_ById
コード:
public static void main(String[] args) throws Exception {
CountryResource.Builder builder = CountryResource.builder();
CountryResource countryResource = builder.authentication(
"[email protected]", "learn"
).build();
System.out.println(
countryResource.getCountry(
Long.valueOf(System.getProperty("countryId"))));
}
国名
フィールドは、JSONで表示されます。
国別パッチ¶
以下のcURLとJavaコマンドで、既存の国の部分編集を行う。 1234
をあなたの国のIDに置き換えてください。
Country_PATCH_ById.sh¶
コマンド:
./Country_PATCH_ById.sh 1234
コード:
curl \
-H "Content-Type: application/json" \
-X PATCH \
"http://localhost:8080/o/headless-admin-address/v1.0/countries/${1}" \
-d "{\"name\": \"Bar\"}" \
-u "[email protected]:learn"
Country_PATCH_ById.java¶
コマンド:
java -classpath .:* -DcountryId=1234 Country_PATCH_ById
コード:
public static void main(String[] args) throws Exception {
CountryResource.Builder builder = CountryResource.builder();
CountryResource countryResource = builder.authentication(
"[email protected]", "learn"
).build();
Country country = countryResource.patchCountry(
Long.valueOf(System.getProperty("countryId")),
new Country() {
{
name = "Bar";
}
});
System.out.println(country);
}
国を指定する¶
以下のcURLとJavaコマンドで、既存の国を完全に上書きします。 1234
をあなたの国のIDに置き換えてください。
Country_PUT_ById.sh¶
コマンド:
./Country_PUT_ById.sh 1234
コード:
curl \
-H "Content-Type: application/json" \
-X PUT \
"http://localhost:8080/o/headless-admin-address/v1.0/countries/${1}" \
-d "{\"a2\": \"AB\", \"a3\": \"ABL\", \"name\": \"Goo\", \"number\": \"1234\"}" \
-u "[email protected]:learn"
Country_PUT_ById.java¶
コマンド:
java -classpath .:* -DcountryId=1234 Country_PUT_ById
コード:
public static void main(String[] args) throws Exception {
CountryResource.Builder builder = CountryResource.builder();
CountryResource countryResource = builder.authentication(
"[email protected]", "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に置き換えてください。
Country_DELETE_ById.sh¶
コマンド:
./Country_DELETE_ById.sh 1234
コード:
curl \
-X DELETE \
"http://localhost:8080/o/headless-admin-address/v1.0/countries/${1}" \
-u "[email protected]:learn"
Country_DELETE_ById.java¶
コマンド
java -classpath .:* -DcountryId=1234 Country_DELETE_ById
コード:
public static void main(String[] args) throws Exception {
CountryResource.Builder builder = CountryResource.builder();
CountryResource countryResource = builder.authentication(
"[email protected]", "learn"
).build();
countryResource.deleteCountry(
Long.valueOf(System.getProperty("countryId")));
}
API Explorer には、 Country
のすべてのサービスとスキーマが表示され、各サービスを試用できるインターフェイスが用意されています。