April 27, 2022
Service builder is most important part in Liferay development. In this post, I am going to talk about Service builder in Liferay 7/DXP. To explain it more effectively, I have taken example of Employee registration. Before we discuss about how use service builder in Liferay 7/DXP, let’s talk about some fact about it.
For more information on service builder, refer following link,
https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/what-is-service-builder
Following are some prerequisite to start implementing service builder example,
Before we discuss steps to implement service builder, it is important for you to understand that Liferay 7/DXP uses OSGI. It makes Liferay 7/DXP modular application. Your service layer and portlets are different OSGI module and both are loosely coupled. You can learn more about OSGI in Liferay from following link,
https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/osgi-and-modularity-for-liferay-6-developers
Following are the steps to generate service/API module using service builder. In next, we will understand how to use service layer module in portlet.
To create a Liferay Workspace in IDE follow the following steps,
Open File Menu → Click on New → Select Liferay Workspace Project. Upon creation of the workspace, the structure should look like below:
a. Right click on Liferay Workspace project in project explorer → Select New → Click on Liferay Module Project. Fill up the detail as follow,
b. Now in Package Name enter appropriate name e.g. “com.enprowess.osgi.example” then click finish.
c. On click of ‘finish’, you will see following folder structure,
Now that you have created skeleton for project. It is time to create service.xml. This is the file where you need to define entity details of your DB table. Following is the default service.xml.
To know more about service builder XML, please refer following link,
https://docs.liferay.com/portal/7.0/definitions/liferay-service-builder_7_0_0.dtd.html
You successfully created service.xml. When you build a service builder it generates classes for service layer. Now it’s time to build service, to run service builder, right click on employee-service folder from project explorer → Select Liferay → Click on build-service.
Congratulation! now you know how to generate service/API module in Liferay 7/DXP. Now let’s understand how you can use service module in portlet module. Here I assume that you already have create portlet module under Employee module.
Update build.gradle file to add service module in dependency. Now gradle will ensure that when you build your portlet module, it will add dependencies mentioned in build file. Following is the snapshot for your reference,
Building portlet module is strait forward and simple if you are using gradle task view. To add gradle task view in your eclipse, Click Window menu → Click on show view option → select gradle task. Following are the steps to build and deploy portlet module.
Congratulation! Now you know steps to implement service builder and use it in portlet.
Blog By,
Zeenesh Patel