Tuesday 4 August 2015

Extend Service Builder with JSON fields

Hello,

   There are lot of articles regarding usage of Liferay's Service Builder - Generating Your Service Layer, Service Builder and Services.
   I want to add few words about extending generated service with own json fields and to describe some differences for Liferay 6.2, which I have faced.

   Let's create some simple portlet who will display some data from service builder at page within JavaScript calls.
   First of all add service.xml with simple entity:
 

  
  

  
  

  
  

  
  
  
  

  
  
   
  
  
 
   After launching service builder you will get generated service layer for this Data entity.
   One of the difference at this step comparing for Liferay 6.2 and previous versions is that service.js file for JavaScript calls is not generated anymore. Later you will see how these calls are made for Liferay 6.2

   Since we will expose data through JavaScript call we have to add required methods to DataServiceImpl.java (for more info about generated classes read here):
public void addData(long groupId, long companyId, String information, String firstName, String lastName) throws SystemException {
   long dataId = CounterLocalServiceUtil.increment(Data.class.getName());
   Data data = DataLocalServiceUtil.createData(dataId);

   data.setGroupId(groupId);
   data.setCompanyId(companyId);

   data.setInformation(information);
   data.setFirstName(firstName);
   data.setLastName(lastName);

   DataLocalServiceUtil.addData(data);

   LOGGER.info("Data: " + information + " has been created.");
  }

  public List<Data> getAllData() throws SystemException {
   return DataUtil.findAll();
  }

   After that relaunch Service Builder, start Liferay and deploy portlet.

    Now try to access http://localhost:8080/api/jsonws?contextPath=/json-service-builder-portlet.
    Here you can see our added methods, in case you will try to invoke some of them you will get "Authenticated access required", since you weren't signed in to portal.


    What if the guest non authorized user should be able to invoke some of the service builder remote methods? In this case simply add annotation @AccessControlled(guestAccessEnabled = true) (here you can read more about it) to required method:
@AccessControlled(guestAccessEnabled = true)
public List<Data> getAllData() throws SystemException {
   return DataUtil.findAll();
  }
    Rebuild services and redeploy portlet. Now guest user can invoke get-all-data method.


    Next step will be invoke get-all-data method from the porlet's page.
    At /api/jsonws you can see different types of invocation.


    In this example we will use JavaScript, add next code to view.jsp:
Result of get-all-data:


  
Liferay.Service( '/json-service-builder-portlet.data/get-all-data', successCallback = function(res) { A.one('#result').html('Success: ' + JSON.stringify(res)); }, exceptionCallback = function(res) { A.one('#result').html('Exception: ' + res); } );
    As the result we will see all the data:


    Let's extend JSON model with fullName property. In order to do this update DataImpl.java with:
        @JSON
 public String fullName;

 public String getFullName() {
  return getFirstName() + " " + getLastName();
 }

 public void setFullName(String fullName) {
  this.fullName = fullName;
 }
   
    Rebuild services and redeploy portlet. Now page will have data with full names. This fullName property will not be stored to database.


   
    Complete code you can find here.
  
BR,
Paul Butenko

2 comments:

  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Liferay, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on Liferay. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.

    For Free Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com


    ReplyDelete
  2. Tutoriel bon et utile. Merci. Nous sommes Liferay Spécialistes Maroc et proposons les services Liferay. Contactez-nous pour plus de détails.

    ReplyDelete