Regions API Basics
Liferay 7.4 U24+ and GA24+
Use Liferay’s REST APIs to create and manage regions.
Adding a Region
Start a new Liferay DXP instance by running
Sign in to Liferay at http://localhost:8080 using the email address test@liferay.com and the password test. When prompted, change the password to learn.
Then, follow these steps:
-
Download and unzip Regions API Basics.
-
Use the cURL script to add a new region to a country. On the command line, navigate to the
curl
folder. Execute theRegion_POST_ToCountry.sh
script.Replace
1234
with a country’s ID. Use Get Countries from Instance to get a list of IDs.The JSON response shows a new Region has been added:
-
The REST service can also be called using the Java client. Navigate out of the
curl
folder and into thejava
folder. Compile the source files: -
Run the
Region_POST_ToCountry.java
class:
Examine the cURL Command
The Region_POST_ToCountry.sh
script calls the REST service with a cURL command.
Here are the command’s arguments:
Arguments | Description |
---|---|
-H "Content-Type: application/json" | Indicates that the request body format is JSON. |
-X POST | The HTTP method to invoke at the specified endpoint |
"http://localhost:8080/o/headless-admin-address/v1.0/countries/${1}/regions" | The REST service endpoint |
-d "{\"name\": \"Foo\", \"regionCode\": \"ABL\"}" | The data you are requesting to post |
-u "test@liferay.com:learn" | Basic authentication credentials |
Basic authentication is used here for demonstration purposes. For production, you should authorize users via OAuth2. See Using OAuth2 to Authorize Users for a sample React application that uses OAuth2.
The other cURL commands use similar JSON arguments.
Examine the Java Class
The Region_POST_ToCountry.java
class adds a region by calling the Region-related service.
This class invokes the REST service using only three lines of code:
Line (abbreviated) | Description |
---|---|
RegionResource.Builder builder = ... | Gets a Builder for generating an RegionResource service instance. |
RegionResource regionResource = builder.authentication(...).build(); | Specifies basic authentication and generates a RegionResource service instance. |
Region region = regionResource.postRegion(...); | Calls the regionResource.postRegion method and passes the data to post. |
Note that the project includes the com.liferay.headless.admin.address.client.jar
file as a dependency. You can find client JAR dependency information for all REST applications in the API explorer in your installation at /o/api
.
The main
method’s comment demonstrates running the class.
The other example Java classes are similar to this one, but call different RegionResource
methods.
See RegionResource for service details.
Below are examples of calling other Region
REST services using cURL and Java.
Get Regions from Instance
You can list regions by executing the following cURL or Java command.
Regions_GET_FromInstance.sh
Command:
Code:
Regions_GET_FromInstance.java
Command:
Code:
The Instance’s Region
objects appear in JSON.
Get a Region
Get a specific region with the following cURL or Java command.
Use Regions_GET_FromInstance.[java|sh]
to get instance Region
IDs.
Region_GET_ById.sh
Command:
Code:
Region_GET_ById.java
Command:
Code:
The Region
fields appear in JSON.
Patch a Region
Do a partial edit of an existing Region with the following cURL and Java commands. Replace 1234
with your Region’s ID.
Region_PATCH_ById.sh
Command:
Code:
Region_PATCH_ById.java
Command:
Code:
Put a Region
Completely overwrite an existing region with the following cURL and Java commands. Replace 1234
with your region’s ID.
Region_PUT_ById.sh
Command:
Code:
Region_PUT_ById.java
Command:
Code:
Delete a Region
Delete an existing region with the following cURL and Java commands. Replace 1234
with your region’s ID.
Region_DELETE_ById.sh
Command:
Code:
Region_DELETE_ById.java
Command
Code:
The API Explorer shows all of the Region
services and schemas and has an interface to try out each service.