Liferay Custom JSON web Services:


1. Create a liferay plugin project and create new service.
In service.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_0_0.dtd">
<service-builder package-path="com.student.slayer">
      <author>sunny</author>
      <namespace>json</namespace>
<entity name="Student" local-service="true" remote-service="true">
            <column name="studentId" type="long" primary="true" />
            <column name="studentName" type="String" />
      </entity>
</service-builder>
2. Run the ant Build service.
3. Write your own method in [Entity]ServiceImpl.
public class StudentServiceImpl extends StudentServiceBaseImpl {
      public com.student.slayer.model.Student getStudent(long studentId)
    throws com.liferay.portal.kernel.exception.PortalException,
          com.liferay.portal.kernel.exception.SystemException {
    return StudentLocalServiceUtil.getStudent(studentId);
}

}
4. Build the service using ant build-service from eclipse ant view.
5. Add the following <servlet> and <servlet-mapping> Entries in web.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>student-portlet</display-name>
<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.servlet.JSONServlet</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>
      <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>
      </jsp-config>
</web-app>
6. Deploy the portlet using ant deploy command from eclipse ant view.
7. Access your json web services by enter the following url:
8. Before entering the url make sure that table must contain the data:
It will display on to the browser like this:

{"studentId":1,"studentName":"sudheer"}


Search This Blog

All the rights are reserved to this blog is belongs to me only.. Powered by Blogger.