Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Using JSON Web Services with liferay

    • 0
    • 3
    • 3
    • 1
    • 2
    • 0
    • 0
    • 0
    • 1.77k
    Comment on it

    Liferay Plugin Portal JSON Web Services :- Liferay support the REST API. By Using That we have to create restful web services and the data format of that is JSON.

    Liferay internally supports to create JSON services. it already have many portal JSON web services, these are simple URL calls by using web services, we can get the JSON data and use any where to consume web services.

    In this tutorial I am guiding you about the Plugin Portal Web Services of Liferay. you can see all portal JSON web service by accessing following URL.

    http://localhost:8080/api/jsonws/

    alt text

    To make a restful web services follow the following steps:-

    1. create service.xml in your plugin portlet project.

    service.xml

    <?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.evon">
        <author>manish</author>
        <namespace>JSONService</namespace>
    
        <entity name="Student" local-service="true" remote-service="true">
    
            <!-- PK fields -->
    
            <column name="studentId" type="long" primary="true" />
    
            <!-- Other fields -->
    
            <column name="name" type="String" />
            <column name="address" type="String" />
    
            <!-- Order -->
    
            <order by="asc">
                <order-column name="name" />
            </order>
    
        </entity>
    
    </service-builder> 
    

    In above code we have created an entity Student and taken some fields like studentId, name and address then build your service.xml. We have seen that the all necessary files are automatically generated in your project after successful build of service.xml.

    2. We need to write java method according to our requirement. We have to decide for which entity we need to provide JSON Web Services and write required java method StudentServiceImpl.java and the StudentServiceImpl class is under package.

    com.evon.service.impl package of your source.

    StudentServiceImpl.java

     public class StudentServiceImpl extends StudentServiceBaseImpl {   
                public  com.evon.model.Student getStudent(
                                        long studentId)
                                        throws com.liferay.portal.kernel.exception.PortalException,
                                                    com.liferay.portal.kernel.exception.SystemException {
                                        return StudentLocalServiceUtil.getStudent(studentId);
                            }
    }
    

    Then we need to run service builder using ant build-service command or from eclipse ant view we can run same command.

    3. Deploy your plugin portlet project into liferay server.

    4. To Access Your web service you can write the following url on your browser.

     http://localhost:8080/api/jsonws/
    

    5. Choose your portlet plugin project from the Context Path drop down list on left hand side of JSONWS API Panel. It can show you the web service method that you have added in the StudentServiceImpl.java file.

    6. Click on the method it will show you the description of method like parameters, return type and exception thrown related to that method.

    7. Go to Execute option of that method and enter the studentId in the text field and click on Invoke it will return the data related to that studentId and also give the complete URL to Access that service.

    alt text

    Thanx....

 2 Comment(s)

  • As per my understanding you are doing on web where as I have posted this blog on json services. You just need to modify service.xml instead of web.xml. only follow the blogs steps no need to modify the web.xml and also check the builder version.

  • after modifying web.xml only i got 3rd point what you written..otherwise getting error like -- Not updating Webservices because version 6.2.2 is newer than version 6.2.0.1 -- sorry any thing wrong..

    http://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/json-web-services

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: