Friday, March 3, 2017

Liferay Custom JSON Web Services


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:-


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



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


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

Total Pageviews

Like Us on Facebook

Number Of Unique Visitor

Free counters!