Liferay Plugin Portal JSON Web Services :- Liferay support the REST API. By Using That we have to create restful web services and the data format of that is JSON.
Liferay internally supports to create JSON services. it already have many portal JSON web services, these are simple URL calls by using web services, we can get the JSON data and use any where to consume web services.
In this tutorial I am guiding you about the Plugin Portal Web Services of Liferay. you can see all portal JSON web service by accessing following URL.
http://localhost:8080/api/jsonws/
To make a restful web services follow the following steps:-
1. create service.xml in your plugin portlet project.
service.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2&_0.dtd">
<service-builder package-path="com.evon">
<author>manish</author>
<namespace>JSONService</namespace>
<entity name="Student" local-service="true" remote-service="true">
<!-- PK fields -->
<column name="studentId" type="long" primary="true" />
<!-- Other fields -->
<column name="name" type="String" />
<column name="address" type="String" />
<!-- Order -->
<order by="asc">
<order-column name="name" />
</order>
</entity>
</service-builder>
In above code we have created an entity Student and taken some fields like studentId, name and address then build your service.xml. We have seen that the all necessary files are automatically generated in your project after successful build of service.xml.
2. We need to write java method according to our requirement. We have to decide for which entity we need to provide JSON Web Services and write required java method StudentServiceImpl.java and the StudentServiceImpl class is under package.
com.evon.service.impl package of your source.
StudentServiceImpl.java
public class StudentServiceImpl extends StudentServiceBaseImpl {
public com.evon.model.Student getStudent(
long studentId)
throws com.liferay.portal.kernel.exception.PortalException,
com.liferay.portal.kernel.exception.SystemException {
return StudentLocalServiceUtil.getStudent(studentId);
}
}
Then we need to run service builder using ant build-service command or from eclipse ant view we can run same command.
3. Deploy your plugin portlet project into liferay server.
4. To Access Your web service you can write the following url on your browser.
http://localhost:8080/api/jsonws/
5. Choose your portlet plugin project from the Context Path drop down list on left hand side of JSONWS API Panel. It can show you the web service method that you have added in the StudentServiceImpl.java file.
6. Click on the method it will show you the description of method like parameters, return type and exception thrown related to that method.
7. Go to Execute option of that method and enter the studentId in the text field and click on Invoke
it will return the data related to that studentId and also give the complete URL to Access that service.
Thanx....
2 Comment(s)