• Rest Extender and JAX-RS Restful Web Service in Liferay 7 DXP


    Rest Extender and JAX-RS Restful Web Service in Liferay 7 Let's us to implement REST Web Service for any table in Liferay's database, Using which we can access various tables in liferay

    Environment
    Liferay IDE 3.1.2 GA3
    Liferay CE Portal Tomcat 7 GA4
    JDK 8
    MySql 5.7

    Step 1 : Create a New Liferay Module Project With Rest Template 




    Step 2 : Give Package name and Component Class Name



    Click on Finish.

    Module Project With following directory structure will be created.


    In my example i am going consume the REST api to retrieve the user tables data.

    com.liferay.portal.remote.cxf.common.configuration.CXFEndpointPublisherConfiguration-cxf.properties
    contextPath=/my-rest-service
    authVerifierProperties=auth.verifier.BasicAuthHeaderAuthVerifier.urls.includes=*
    

    com.liferay.portal.remote.rest.extender.configuration.RestExtenderConfiguration-rest.properties
    contextPaths=/my-rest-service
    jaxRsServiceFilterStrings=(component.name=com.liferaystack.application.MyRestServiceApplication)
    

    Note
    1. If you want to change the context path of your module, you can use the above properties files.
    2. Include compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0" in build.gradle to access User Table API from Liferay's Services.
    3. If You want to implement same REST Service for custom table (entity) please add the dependency in build.gradle

    MyRestServiceApplication.java
    This is the REST Controller Where we can write out REST API Code, I have removed the created template code and added the below code into this file
    package com.liferaystack.application;
    
    import com.liferay.portal.kernel.dao.orm.QueryUtil;
    import com.liferay.portal.kernel.exception.PortalException;
    import com.liferay.portal.kernel.log.Log;
    import com.liferay.portal.kernel.log.LogFactoryUtil;
    import com.liferay.portal.kernel.model.User;
    import com.liferay.portal.kernel.service.UserLocalServiceUtil;
    import com.liferay.portal.kernel.util.ContentTypes;
    import com.liferay.portal.kernel.util.Validator;
    
    import java.util.Collections;
    import java.util.List;
    import java.util.Set;
    
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.QueryParam;
    import javax.ws.rs.core.Application;
    
    import org.osgi.service.component.annotations.Component;
    
    /**
     * @author Syed Ali
     */
    
    @ApplicationPath("/users")
    @Component(immediate = true, service = Application.class)
    public class MyRestServiceApplication extends Application {
    
     public Set<Object> getSingletons() {
      return Collections.<Object>singleton(this);
     }
    
     @GET
     @Produces("text/plain")
     public String working() {
      return "Web Service Works!";
     }
    
     @GET
     @Path("/getalluser")
     @Produces(ContentTypes.TEXT_PLAIN)
     public String hello() {
      List<User> users = UserLocalServiceUtil.getUsers(QueryUtil.ALL_POS,QueryUtil.ALL_POS);
      return users.toString();
     }
    
     @GET
     @Path("/getuser/{userId}")
     @Produces(ContentTypes.TEXT_PLAIN)
     public String morning(
      @PathParam("userId") long userId,
      @QueryParam("param") String param) {
    
      User user = null;
      try {
       user = UserLocalServiceUtil.getUser(userId);
      } catch (PortalException e) {
       _log.error("PortalException : "+e.getMessage()); 
      }
    
      if(Validator.isNull(user)){
       return "[No User Found With User Id : "+userId+"]";
      }
      return user.toString();
     }
    
     private static Log _log = LogFactoryUtil.getLog(MyRestServiceApplication.class);
    }
    

    Deploy the module to your and Test the REST Web Service

    Testing the Web Service


    Accessing the Root Context
    Goto http://localhost:8080/o/my-rest-service/ it will show the list of services available

    Accessing Rest Controller
    Goto http://localhost:8080/o/my-rest-service/users it will show the list of services available

    Get All Users Using Rest Controller
    Goto http://localhost:8080/o/my-rest-service/users/getalluser you can fetch all users

    Get Users with userId Using Rest Controller
    Goto http://localhost:8080/o/my-rest-service/users/getuser/20156 you can fetch users with userId 20156

    Source Code


  • 5 comments:

    1. I'm getting Invocation of 'addService' failed. exception in this

      ReplyDelete
    2. I can't find the folder Configuration under resources. I'm working with liferay DXP 7.3.5.
      Does the rest extender works in the 7.3 version ?
      If not, what is the alternative ?

      ReplyDelete
    3. I can't find the folder Configuration under resources. I'm working with liferay DXP 7.3.5.
      Does the rest extender works in the 7.3 version ?
      If not, what is the alternative ?

      ReplyDelete