Asset Publisher

Expert Insights Hub

Stay connected with us to discover what top tech minds think about cutting-edge trends, development platforms, team hiring, automation, and innovations shaping the industry. Gain valuable knowledge from industry leaders, explore unique strategies, and stay ahead in the ever-evolving world of technology. Stay informed, stay inspired, and stay ahead of the competition with our exclusive updates!

Blogs

CRUD Operation with Headless REST API in Liferay

Caption

Overview:

A Headless REST API refers to an architecture that allows systems to interact with each other without being tied to a front-end user interface. This is particularly useful in enterprise applications, where the back-end data needs to be accessed by different front-end systems or third-party applications. 

Using Headless REST APIs, developers can perform CRUD operations (Create, Read, Update, Delete) on Liferay’s resources without relying on its traditional user interface. This flexibility enables integration with external systems, custom front-end applications, mobile apps, and IoT devices. 

Prerequisite:

  • JDK 11 
  • Liferay Server: liferay-dxp-tomcat-7.4.13.u92 
  • Liferay Developer Studio:  3.9.7.202207131011-ga8

Headless REST API:
A Headless API is an interface that allows you to interact with a system’s data programmatically, without requiring a user interface (UI). In a headless environment, the front-end (UI) is independent of the back-end, and communication occurs via APIs, making it easier to integrate with mobile apps, web apps, or other services. 

A Headless REST API in Liferay refers to a set of API endpoints that allow for the interaction with the Liferay platform without using its user interface (UI). It allows you to manage and retrieve data, content, and other resources from Liferay in a "headless" manner, meaning there's no need for a front-end web interface directly tied to the API's operations. This API is designed to be consumed by external systems or client-side applications (e.g., mobile apps, single-page applications, etc.). 

Key Features of Headless API:

  1. Decoupled Front-End and Back-End: Headless APIs enable a decoupled architecture where the frontend and backend are separated. This allows developers to build user interfaces independently from the Liferay platform, offering greater flexibility and scalability. 
  2. JSON-Based Data Exchange: The API communicates using JSON (JavaScript Object Notation) to exchange data between client and server. This ensures a lightweight, easy-to-parse data format that works well with modern web applications. 
  3. RESTful Design: Liferay’s Headless APIs follow REST (Representational State Transfer) principles, ensuring a standard way to interact with the backend. This provides stateless operations using HTTP methods like GET, POST, PUT, and DELETE. 
  4. Scalability and Flexibility: Liferay’s headless APIs allow for easy scaling, enabling integrations with various third-party systems, and supporting large-scale, distributed solutions. 
  5. Content and Document Management: Provides powerful APIs for managing content types (web content, blogs, etc.) and handling document storage, retrieval, and manipulation in the Document and Media Library. 
  6. Security & Authentication: Liferay Headless APIs offer robust authentication mechanisms such as OAuth2, JWT, and Basic Authentication to ensure secure communication between the client and the API. 
  7. Custom Resource Extensions: Developers can extend Liferay’s headless API by adding custom resources and endpoints, enabling specialized business logic and personalized integrations. 
  8. Integration with Third-Party Systems: Seamlessly integrates with external systems like CRMs, ERPs, and other applications, enabling a unified ecosystem and data exchange without a traditional UI.

Steps for Performing CRUD Operations with Liferay Headless REST API

To perform CRUD (Create, Read, Update, Delete) operations using Liferay Headless APIs, you’ll interact with specific endpoints for each operation. Below are the steps for each operation: 

1. Start the Liferay Developer Studio: 

  • Open Liferay Developer Studio
  • Start the Liferay server by navigating to the Servers tab and clicking on the Start button.

2. Create a Workspace Project: 

  • Open Liferay Developer Studio and start the server. 
  • Navigate to FileNewLiferay Workspace Project
  • Provide a project name (e.g., global-workspace) and click Finish.

3. Create a Service Builder 

  • Go to File → New → Liferay Module Project
  • Choose the Service Builder template, provide the project name (e.g., employee-service), and click Finish
  • Enter the class and package name. 
  • Modify the service.xml file to define an entity (e.g. Employee) with attributes. Set the remote-service attribute to true.

4. Build the ServiceBuilder Module

  • Open Gradle Task Executor in Liferay Developer Studio. 
  • In the Gradle Task Executor, navigate to Modules. 
  • Go to the Employee Service module. 
  • Under the Employee Service module, go to the Build section. 
  • Click on Build Service to execute the build task.


 

  • Refresh the Gradle project to generate necessary packages and classes.

     

5. Implement business logic for (CRUD) Create, Read, Update, Delete in service builder.

  • Create: Add logic to save a new entity (e.g., an employee) to the database.

     

  • Read: Fetch all employees.

     

  • Update: Modify an existing employee by employeeId.

     

  • Delete: Remove an employee by employeeId.

6. Create and Develop the REST Builder Module

  • Go to File → New → Liferay Module Project and select the REST Builder template. 
  • Provide a project name (e.g., employee-rest) and click Finish
  • Enter the class and package name. 
  • Add the OpenAPI configuration in the rest-openapi.yaml file under the *-impl module’s root folder.









     

7. Generate Scaffolding:

  • Open Gradle Task Executor in Liferay Developer Studio. 
  • In the Gradle Task Executor, navigate to Modules.
  • Go to the employee-rest module
  • Under the E employee-rest module, go to the employee-service-impl section. 
  • In the employee-service-impl module, go to the Build section. 
  • Click on buildRest to execute the build task.

     
  • Refresh the Gradle project to generate necessary packages and classes. 

8. Add employee-service Dependency to employee-rest Project:

  • Navigate to the build.gradle file of the employee-rest-impl project in your project structure. 
  • Add the employee-service Dependency

     
  • Refresh Gradle
  • Save the changes and sync the Gradle project. In your IDE, click on Sync Gradle Project 

9. Now we have performed the CRUD operation for Employee in OpenAPI and implemented Business Logic for each service. 

  • Add Employee



     
  • Get All Employees

     
  • Update Employee

     
  • Delete Employee

     

10. Deploy and Test the API:

  • Deploy the REST Builder module to the Liferay server.

     

  • The API is available at the following URL: http://localhost:8080/o/api

     

  • Add an Employee:



     

  • Retrieve All Employees:

     

  • Update an Employee:

     

  • Delete an Employee


  • If you have any query, click here

More Blog Entries