05 January, 2016

JSON Web Service

Friends,

You may be aware that Liferay provides facility to expose remote service in form of REST and SOAP.


Here I am going to show the steps how to expose JSON Web service in Liferay:

1) Create Liferay MVC Portlet
2)  By using Liferay Service Builder concept, design service.xml given as below :
<?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.jignesh.json.service">
 <author>jignesh</author>
 <namespace>PersonalDetail</namespace>
 <entity name="PersonalDetail" local-service="true" remote-service="true">
  <!-- PK fields -->
  <column name="personId" type="long" primary="true" />
  <!-- Group instance -->
  <column name="groupId" type="long" />
  <!-- Other fields -->
  <column name="firstName" type="String" />
  <column name="lastName" type="String" />
  <!-- Order -->
  <order by="asc">
   <order-column name="firstName" />
  </order>
  <!-- Finder methods -->
  <finder name="firstName" return-type="Collection">
   <finder-column name="firstName" />
   <finder-column name="groupId" />
  </finder>
  <finder name="lastName" return-type="Collection">
   <finder-column name="lastName" />
  </finder>
 </entity>
</service-builder>

3) Build service and deploy the portlet
4) Add below code into web.xml of portlet :
<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Jignesh</display-name>
            <filter>
         <filter-name>Secure JSON Web Service Servlet Filter</filter-name>
         <filter-class>com.liferay.portal.kernel.servlet.PortalClassLoaderFilter</filter-class>
         <init-param>
          <param-name>filter-class</param-name>
          <param-value>com.liferay.portal.servlet.filters.secure.SecureFilter</param-value>
         </init-param>
         <init-param>
          <param-name>basic_auth</param-name>
          <param-value>true</param-value>
         </init-param>
         <init-param>
          <param-name>portal_property_prefix</param-name>
          <param-value>jsonws.servlet.</param-value>
         </init-param>
       </filter>

       <filter-mapping>
         <filter-name>Secure JSON Web Service Servlet Filter</filter-name>
         <url-pattern>/api/jsonws/*</url-pattern>
       </filter-mapping>
       <servlet>
         <servlet-name>JSON Web Service Servlet</servlet-name>
         <servlet-class>com.liferay.portal.kernel.servlet.PortalClassLoaderServlet</servlet-class>
         <init-param>
          <param-name>servlet-class</param-name>
          <param-value>com.liferay.portal.jsonwebservice.JSONWebServiceServlet</param-value>
         </init-param>
         <load-on-startup>0</load-on-startup>
       </servlet>
       <servlet-mapping>
         <servlet-name>JSON Web Service Servlet</servlet-name>
         <url-pattern>/api/jsonws/*</url-pattern>
       </servlet-mapping>
            <jsp-config>
                        <taglib>
                                    <taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>
                                    <taglib-location>
                                                /WEB-INF/tld/liferay-portlet.tld
                                    </taglib-location>
                        </taglib>
                        <taglib>
                                    <taglib-uri>http://liferay.com/tld/aui</taglib-uri>
                                    <taglib-location>/WEB-INF/tld/aui.tld</taglib-location>
                        </taglib>
            </jsp-config>
</web-app>

3) Add below code in <Entity name>ServiceImpl (like PersonalDetailServiceImpl) :
public  com.jignesh.json.service.model.PersonalDetail getPersonalDetail(
            long personId)
            throws com.liferay.portal.kernel.exception.PortalException,
                        com.liferay.portal.kernel.exception.SystemException {
            return PersonalDetailLocalServiceUtil.getPersonalDetail(personId);

4) Build service again and deploy the portlet which will create singnature entry of this method in Util class and can be accessible.

5) Now hit the URL localhost:8080/localhost:8080/json-portlet/api/jsonws
here json-portlet would be the name of your portlet context

6) Once you hit URL, you will be able to see below screen where you can check the exposed json web service

7) Here you can enter proper value of personId and can the result as shown in below snap:

Here you can access json exposed service in 3 different way:
Using Java script :

Liferay.Service(
  '/json-portlet.personaldetail/get-personal-detail',
  {
    personId: 1
  },
  function(obj) {
    console.log(obj);
  }
);
Using Shell command:

curl http://localhost:8080/api/jsonws/json-portlet.personaldetail/get-personal-detail \
  -u test@liferay.com:test \
  -d personId=1
Using REST call via URL:
http://localhost:8080/api/jsonws/json-portlet.personaldetail/get-personal-detail/person-id/1

Popular Posts

Featured Post

Liferay 7.3 compatibility matrix

Compatibility Matrix Liferay's general policy is to test Liferay Portal CE against newer major releases of operating systems, open s...