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
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
<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
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
public void sayHello(Striang name) | |
{ | |
System.out.println("Hello " + name); | |
} |
STEP:4 Build the service.
STEP:5 Add the method definition to SampleServiceImpl class
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
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 -
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
<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> |
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!!!
good
ReplyDelete