Categories and Vocabulary API Basics
Liferay’s REST APIs provide services for Liferay’s categories and vocabularies functionality. You can create and edit vocabularies with the API. You can also associate and edit categories with the API. Start by seeing an example of adding a new vocabulary.
Adding a Vocabulary
Start a new Liferay DXP instance by running
docker run -it -m 8g -p 8080:8080 liferay/dxp:7.4.13-u75
Sign in to Liferay at http://localhost:8080 using the email address [email protected] and the password test. When prompted, change the password to learn.
Then, follow these steps:
Download and unzip Categories and Vocabulary API Basics.
curl https://resources.learn.liferay.com/dxp/latest/en/content-authoring-and-management/tags-and-categories/developer-guide/liferay-f5w3.zip -O
unzip liferay-f5w3.zip
Find your Site’s ID. You’ll use this in different service calls below.
Use the cURL script to add a new vocabulary to your Site. On the command line, navigate to the
curl
folder. Execute theTaxonomyVocabulary_POST_ToSite.sh
script with your Site ID as a parameter../TaxonomyVocabulary_POST_ToSite.sh 1234
The JSON response shows a new vocabulary has been added:
{ "availableLanguages" : [ "en-US" ], "creator" : { "additionalName" : "", "contentType" : "UserAccount", "familyName" : "Test", "givenName" : "Test", "id" : 20129, "name" : "Test Test", "profileURL" : "/web/test" }, "dateCreated" : "2021-09-09T21:03:15Z", "dateModified" : "2021-09-09T21:03:15Z", "description" : "Foo", "id" : 40126, "name" : "Able", "numberOfTaxonomyCategories" : 0, "siteId" : 20125 }
Go to the Categories application by navigating to Administration Menu → Categorization → Categories. See that a new vocabulary 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 with the following command:javac -classpath .:* *.java
Run the
TaxonomyVocabulary_POST_ToSite
class with the following command. Replace thesiteId
value with your Site’s ID:java -classpath .:* -DsiteId=1234 TaxonomyVocabulary_POST_ToSite
Examine the cURL Command
The TaxonomyVocabulary_POST_ToSite.sh
script calls the REST service with a cURL command.
curl \
-H "Content-Type: application/json" \
-X POST \
"http://localhost:8080/o/headless-admin-taxonomy/v1.0/sites/${1}/taxonomy-vocabularies" \
-d "{\"description\": \"Foo\", \"name\": \"Able\"}" \
-u "[email protected]:learn"
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-taxonomy/v1.0/sites/${1}/taxonomy-vocabularies" |
The REST service endpoint |
-d "{\"description\": \"Foo\", \"name\": \"Able\"}" |
The data you are requesting to post |
-u "[email protected]: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 TaxonomyVocabulary_POST_ToSite.java
class adds a vocabulary by calling the vocabulary related service.
public static void main(String[] args) throws Exception {
TaxonomyVocabularyResource.Builder builder =
TaxonomyVocabularyResource.builder();
TaxonomyVocabularyResource taxonomyVocabularyResource =
builder.authentication(
"[email protected]", "learn"
).build();
TaxonomyVocabulary taxonomyVocabulary =
taxonomyVocabularyResource.postSiteTaxonomyVocabulary(
Long.valueOf(System.getProperty("siteId")),
new TaxonomyVocabulary() {
{
description = "Foo";
name = "Baker";
}
});
This class invokes the REST service using only three lines of code:
Line (abbreviated) | Description |
---|---|
TaxonomyVocabularyResource.Builder builder = ... |
Gets a Builder for generating a TaxonomyVocabularyResource service instance. |
TaxonomyVocabularyResource taxonomyVocabularyResource = builder.authentication(...).build(); |
Specifies basic authentication and generates a TaxonomyVocabularyResource service instance. |
TaxonomyVocabulary taxonomyVocabulary = taxonomyVocabularyResource.postSiteTaxonomyVocabulary(...); |
Calls the postSiteTaxonomyVocabulary method and passes the data to post. |
Note that the project includes the com.liferay.headless.admin.taxonomy.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 TaxonomyVocabularyResource
methods.
See TaxonomyVocabularyResource for service details.
Below are examples of calling other TaxonomyVocabulary
REST services using cURL and Java.
Get Vocabularies from Site
You can list a Site’s vocabularies by executing the following cURL or Java command. As above, replace 1234
with your Site’s ID.
TaxonomyVocabularies_GET_FromSite.sh
Command:
./TaxonomyVocabularies_GET_FromSite.sh 1234
Code:
curl \
"http://localhost:8080/o/headless-admin-taxonomy/v1.0/sites/${1}/taxonomy-vocabularies" \
-u "[email protected]:learn"
TaxonomyVocabularies_GET_FromSite.java
Command:
java -classpath .:* -DsiteId=1234 TaxonomyVocabularies_GET_FromSite
Code:
public static void main(String[] args) throws Exception {
TaxonomyVocabularyResource.Builder builder =
TaxonomyVocabularyResource.builder();
TaxonomyVocabularyResource taxonomyVocabularyResource =
builder.authentication(
"[email protected]", "learn"
).build();
Page<TaxonomyVocabulary> page =
taxonomyVocabularyResource.getSiteTaxonomyVocabulariesPage(
Long.valueOf(System.getProperty("siteId")), null, null,
Pagination.of(1, 2), null);
The Site’s TaxonomyVocabulary
objects are listed in JSON.
Get a Vocabulary
Get a specific vocabulary with the following cURL or Java command. Replace 1234
with the vocabulary’s ID.
Use TaxonomyVocabularies_GET_FromSite.[java|sh]
to get Vocabulary
IDs.
TaxonomyVocabulary_GET_ById.sh
Command:
./TaxonomyVocabulary_GET_ById.sh 1234
Code:
curl \
"http://localhost:8080/o/headless-admin-taxonomy/v1.0/taxonomy-vocabularies/${1}" \
-u "[email protected]:learn"
TaxonomyVocabulary_GET_ById.java
Command:
java -classpath .:* -DtaxonomyVocabularyId=1234 TaxonomyVocabulary_GET_ById
Code:
public static void main(String[] args) throws Exception {
TaxonomyVocabularyResource.Builder builder =
TaxonomyVocabularyResource.builder();
TaxonomyVocabularyResource taxonomyVocabularyResource =
builder.authentication(
"[email protected]", "learn"
).build();
System.out.println(
taxonomyVocabularyResource.getTaxonomyVocabulary(
Long.valueOf(System.getProperty("taxonomyVocabularyId"))));
}
The TaxonomyVocabulary
fields are listed in JSON.
Patch a Vocabulary
Do a partial edit of an existing vocabulary with the following cURL and Java commands. Note, replace 1234
with your vocabulary’s ID.
TaxonomyVocabulary_PATCH_ById.sh
Command:
./TaxonomyVocabulary_PATCH_ById.sh 1234
Code:
curl \
-H "Content-Type: application/json" \
-X PATCH \
"http://localhost:8080/o/headless-admin-taxonomy/v1.0/taxonomy-vocabularies/${1}" \
-d "{\"description\": \"Bar\", \"name\": \"Able\"}" \
-u "[email protected]:learn"
TaxonomyVocabulary_PATCH_ById.java
Command:
java -classpath .:* -DtaxonomyVocabularyId=1234 TaxonomyVocabulary_PATCH_ById
Code:
public static void main(String[] args) throws Exception {
TaxonomyVocabularyResource.Builder builder =
TaxonomyVocabularyResource.builder();
TaxonomyVocabularyResource taxonomyVocabularyResource =
builder.authentication(
"[email protected]", "learn"
).build();
TaxonomyVocabulary taxonomyVocabulary =
taxonomyVocabularyResource.patchTaxonomyVocabulary(
Long.valueOf(System.getProperty("taxonomyVocabularyId")),
new TaxonomyVocabulary() {
{
description = "Bar";
name = "Baker";
}
In this example the description is changed from Foo to Bar.
Put a Vocabulary
Overwrite an existing vocabulary with the following cURL and Java commands. Note, replace 1234
with your vocabulary’s ID.
TaxonomyVocabulary_PUT_ById.sh
Command:
./TaxonomyVocabulary_PUT_ById.sh 1234
Code:
curl \
-H "Content-Type: application/json" \
-X PUT \
"http://localhost:8080/o/headless-admin-taxonomy/v1.0/taxonomy-vocabularies/${1}" \
-d "{\"description\": \"Goo\", \"name\": \"Able\"}" \
-u "[email protected]:learn"
TaxonomyVocabulary_PUT_ById.java
Command:
java -classpath .:* -DtaxonomyVocabularyId=1234 TaxonomyVocabulary_PUT_ById
Code:
public static void main(String[] args) throws Exception {
TaxonomyVocabularyResource.Builder builder =
TaxonomyVocabularyResource.builder();
TaxonomyVocabularyResource taxonomyVocabularyResource =
builder.authentication(
"[email protected]", "learn"
).build();
TaxonomyVocabulary taxonomyVocabulary =
taxonomyVocabularyResource.putTaxonomyVocabulary(
Long.valueOf(System.getProperty("taxonomyVocabularyId")),
new TaxonomyVocabulary() {
{
description = "Goo";
name = "Baker";
}
});
System.out.println(taxonomyVocabulary);
}
Delete a Vocabulary
Delete an existing vocabulary with the following cURL and Java commands. Note, replace 1234
with your vocabulary’s ID.
TaxonomyVocabulary_DELETE_ById.sh
Command:
./TaxonomyVocabulary_DELETE_ById.sh 1234
Code:
curl \
-X DELETE \
"http://localhost:8080/o/headless-admin-taxonomy/v1.0/taxonomy-vocabularies/${1}" \
-u "[email protected]:learn"
TaxonomyVocabulary_DELETE_ById.java
Command
java -classpath .:* -DtaxonomyVocabularyId=1234 TaxonomyVocabulary_DELETE_ById
Code:
public static void main(String[] args) throws Exception {
TaxonomyVocabularyResource.Builder builder =
TaxonomyVocabularyResource.builder();
TaxonomyVocabularyResource taxonomyVocabularyResource =
builder.authentication(
"[email protected]", "learn"
).build();
taxonomyVocabularyResource.deleteTaxonomyVocabulary(
Taxonomy Category Services
The cURL commands and Java classes for taxonomy categories work similarly to taxonomy vocabularies. Note that some services require the taxonomy vocabulary ID.
Files | Description |
---|---|
TaxonomyCategories_GET_FromTaxonomyVocabulary.[java\|sh] |
Get a list of categories from a vocabulary. |
TaxonomyCategory_DELETE_ById.[java\|sh] |
Delete a category. |
TaxonomyCategory_GET_ById[java\|sh] |
Get a specific category by ID. |
TaxonomyCategory_PATCH_ById.[java\|sh] |
Patch a category. |
TaxonomyCategory_POST_ToTaxonomyVocabulary.[java\|sh] |
Post a category to a vocabulary. |
TaxonomyCategory_PUT_ById.[java\|sh] |
Put a category. |
The API Explorer lists all of the TaxonomyVocabulary
and TaxonomyCategory
services and schemas and has an interface to try out each service.