Expose Liferay & Custom Services as JSON

Copyright graur razvan ionut @freedigitalphotos.net

Well, this was not the easiest part last time. Honestly, i like the way alfresco does exchange data between its Alfresco-Share application and the repository. REST-Calls in JSON-Format is the most easist way to work out customers requirements. Just evaluating the body of the request/response and you can accessing data without marshelling, xml-validation and so on …

Now, in my current Liferay-Project i´d like to do in the same  way to increase the productivity. And yes after some obstacles its pretty easy to reach this using Liferay - its almost easier as with Alfresco ;-) But keep looking on the following lines …

Remote Services with Liferay are pretty simple to develop and expose using the Service-Builder. This feature is responsible to create (like ejb 2.1 and behyond) the local and remote stuff we´d like to use. Persistence and transaction-management comes along the way as a pretty nice feature - you can define a mapping according to your database-tables.

But i wont show you the way to persist data - i want to show you on how you can  expose business logic to (legacy) systems using JSON as a response-format.

First of all we have to setup  our SDK and EXT-Environment. We create via create-batch file a new ext-project and with that project we add a service.xml in ext-impl/src/com/wps/liferay/service.xml

<?xml version="1.0"?><service-builder package-path="com.wps.liferay"> <namespace>WPS</namespace>

 <entity name="SessionValidationManager" local-service="true" remote-service="true"/>

</service-builder>

Now add to your ant-build file build.xml following property to select the service-file:

<property name="service.file" value="docroot/WEB-INF/com/wps/liferay/service.xml"></property>

Process the ant-tash build-service to create all that kind of stuff like a LocalService and a RemoteService. Refresh your workspace if youre using an IDE like Eclipse.

Now you have a lot of new classes located in ext-impl as well as in ext-service. Now you can add your business-logic to your classes that are names like SessionValidationManagerServiceImpl.java (EntityNameServiceImpl). In my example i add following method to that class (its still empty if you open it the first time):

	public JSONObject validateUser(String String userId) {

		// die some cool logic

		return jsonObject;
	}

Now, to propagate our businesslogic to the world-wide web, we have to start the “build service” task again. Dont worry about it, if you have done all correctly the service builder is smart enough to generate automatically all the implementation stuff that would rather than depend on you!

Refresh youre workspace again!

Now, please see the Util Class of your Entity (e.g. SessionValidationManagerServiceUtil) and you´l find your new facade method.

Thats all!

Deploy your code and open your browser. Type following into the browser:

http://localhost:8080/tunnel-web/secure/json?serviceClassName=com.wps.liferay.service.SessionValidationManagerServiceUtil&serviceMethodName=validateUser&serviceParameters=[userId]&userId=karlschmidt

Comments

2 Responses to “Expose Liferay & Custom Services as JSON”

  1. Dennis Gearon on January 4th, 2012 5:59 am

    So where is the com.wps.liferay libraries? Who is wps.com? They don’t seem to be Java oriented :-)

  2. Wenzky Sebastian on January 13th, 2012 8:30 pm

    Thats just a simple excerpt how to start with Liferay Service Builder. There are more steps to take and it requires to have a more deeper expierience in Liferay.

Leave a Reply




Security Code: