Thursday, August 23, 2012

Liferay Custom JSON Web Service Development



Following is the step by step description of generating a custom liferay plugin service and exposing it as JSON Web Service.



STEP:1 Create a liferay plugin project and create new service


Here is the sample service.xml


<service-builder package-path="com.test.sample">
<author>harish.kumar</author>
<namespace>sample</namespace>
<entity name="Sample" local-service="true" remote-service="true">
<!-- PK fields -->
<column name="sampleId" type="long" primary="true" />
<!-- Other fields -->
<column name="name" type="String" />
<column name="address" type="String" />
</entity>
</service-builder>

Make sure remote-service=”true” in entity tag declaration.



STEP:2  Build the service.



STEP:3  Add your custom method in SampleLocalServiceImpl class


public void sayHello(Striang name)
{
System.out.println("Hello " + name);
}

STEP:4  Build the service.



STEP:5  Add the method definition to SampleServiceImpl class


public void sayHello(String name)
{
SampleLocalServiceUtil.sayHello(name);
}

STEP:6  build the service.



STEP:7  Add the following <servlet> and <servlet-mapping> Entries to portlet's web.xml file -


<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>
<servlet-mapping>
<servlet-name>JSON Web Service Servlet</servlet-name>
<url-pattern>/api/secure/jsonws/*</url-pattern>
</servlet-mapping>
view raw json-web-xml hosted with ❤ by GitHub

STEP:8  deploy the portlet.


STEP:9  To access your json web services enter the following url -

http://localhost:8080/<<portlet-context>>/api/jsonws





and Its Done!!!





1 comment: