Creating Service Wrappers
With Service Wrappers, you can override default service methods to add extra functionality. For example, you may want the value of a field you’ve added to Liferay’s User
object to be saved whenever the Liferay API’s addUser
or updateUser
methods are called. Liferay’s service wrappers provide easy-to-use extension points for customizing Liferay’s services.
Deploying a Service Wrapper
Start a new Liferay instance by running
Sign in to Liferay at http://localhost:8080. Use the email address test@liferay.com and the password test. When prompted, change the password to learn.
Then, follow these steps to deploy the example:
-
Download and unzip the
liferay-j1c2.zip
example project. -
Build and deploy the project module.
NoteThis command is the same as copying the deployed jars to
/opt/liferay/osgi/modules
on the Docker container. -
Confirm the deployment in the Liferay Docker container console.
-
To verify the example module’s customization, open your browser to
https://localhost:8080
. -
Log out of Liferay and log back in. The service wrapper prints this message to the Liferay Docker container console:
This example prints a message to the console whenever the authenticateByEmailAddress
method is called.
Creating a Service Wrapper Class
- Choose the service you want to wrap. This example creates a service wrapper for
UserLocalService
, so it extendsUserLocalServiceWrapper
:
- Annotate the class so Liferay knows this is a service wrapper component.
- Choose the method you want to override and add your own implementation.
Overriding a Method
-
Open the
J1C2UserLocalServiceWrapper
class in your text editor or IDE. -
Inside the class, create a public method called
getUser
. This overridesUserLocalService
’sgetUser
method. As such, it must pass along
as an argument and return aUser
. This method should also throw aPortalException
. Make sure to add the@Override
annotation. -
Since this method returns the
User
object, import it at the top of the file. -
Create a similar method to the one in the example so that every time the
getUser
method is called, it prints a message to the console. -
You still want
getUser
to be called, so make the method return the results of callinggetUser
’s super method. -
In the end, your method should look like this:
-
Build and deploy your module.
Testing Your Service Wrapper
-
Go back to
https://localhost:8080
. -
Click My Profile in the User menu. This takes you to your profile page.
-
When you open your profile page, Liferay calls the
getUser
method a couple times. Check your console for the following message:
Congratulations! You’ve customized a service’s methods using a Liferay service wrapper.