Liferay give you JSON web services for its entities like groups, organization etc . You can see all those services by this URL http://localhost:8080/api/jsonws .
But today we will create our own Entity like Student and access the methods by URLs and get JSON response. So Lets start this step by step:-
Step 1:-Create Service Builder
My database table name is student with this data:-
And my service.xml is:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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.services"> | |
<author>aditya.bhardwaj</author> | |
<namespace>restapi</namespace> | |
<entity name="Student" table="student" local-service="true" remote-service="true"> | |
<column name="sid" type="long" primary="true" id-type="increment"></column> | |
<column name="studentName" type="String"></column> | |
<column name="address" type="String"></column> | |
<column name="phone" type="String"></column> | |
</entity> | |
</service-builder> |
Now Build Services.
Explanation:-
We already discuss how to create service builder in Liferay .
Here we also provide tag remote-service="true" which is needed for Rest Services.
Step 2:-Change xxxServiceImpl
When you build services it will create a xxxServiceImpl class .Now create your method which you want as JSON API in StudentServiceImpl :-
StudentServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.services.service.impl; | |
import com.liferay.portal.kernel.exception.PortalException; | |
import com.liferay.portal.kernel.exception.SystemException; | |
import com.liferay.portal.security.ac.AccessControlled; | |
import com.services.model.Student; | |
import com.services.service.StudentLocalServiceUtil; | |
import com.services.service.base.StudentServiceBaseImpl; | |
public class StudentServiceImpl extends StudentServiceBaseImpl { | |
@AccessControlled(guestAccessEnabled = true) | |
public Student getStudentDetail(long sid) throws PortalException, SystemException{ | |
return StudentLocalServiceUtil.getStudent(sid); | |
} | |
} |
Now again Build Services.
Explanation:-
1)Here we create getStudentDetail which internally call method from Util Class so basically we expose our method For Rest API.
2)We annotate our method with:-
@AccessControlled(guestAccessEnabled = true)
so that you can access this method without login. If you want that no one access this method without login simply remove this annotation.
Step 3:-Check Output
Now deploy your portlet and hit http://localhost:8080/api/jsonws in your browser and select your project from context path:-
Now Click on your method get-student-detail
provide studentId and click on Invoke:-
You will see the Result. Now you can click on JavaScipt Example:-
URL Example:-
You can directly hit this URL in browser and see the Json response.
Step 5:-Access in Jsp
You can access the JSON rest web services in jsp as:-
view.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %> | |
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> | |
<portlet:defineObjects /> | |
<aui:form method="GET" name="<portlet:namespace />fm"> | |
<aui:fieldset> | |
<aui:input label="Student ID" name="student-ID"></aui:input> | |
</aui:fieldset> | |
<aui:button-row><aui:button id="get-student-detail" value="Get Student Detail"></aui:button></aui:button-row> | |
</aui:form> | |
<aui:script use="node, event"> | |
var getStudentDetail = A.one('#get-student-detail'); | |
var studentIDNode = A.one('#<portlet:namespace />student-ID'); | |
getStudentDetail.on('click', function(event) { | |
var studentID = studentIDNode.get('value'); | |
var studentDetail = Liferay.Service( | |
'/restAPI-portlet.student/get-student-detail', | |
{ | |
sid: studentID | |
}, | |
function(obj) { | |
alert("Details=>"+obj.studentName+"-"+obj.address); | |
console.log(obj); | |
} | |
); | |
}); | |
</aui:script> |
Deploy your portlet and add to page. Now enter student id and click on Get Student Detail Button:-
You can Download Source code from Liferay Custom JSON Web Services.
No comments:
Post a Comment