Forums

Home » Liferay Portal » English » Liferay Legacy

Combination View Flat View Tree View
Threads [ Previous | Next ]
Sebastián Gurin
REST-style JSON Web Services
September 3, 2007 11:52 AM
Answer

Sebastián Gurin

Rank: Junior Member

Posts: 75

Join Date: June 13, 2007

Recent Posts

Hi all. I'm trying to access portal's REST-style JSON Web Services from javascript.

First of all, looking in liferay 4.3.1 sourcecode (com.liferay.portal.action.JSONServiceAction.java, com.liferay.portlet.documentlibrary.service.http.*JSON.java, struts-config.xml) and I was able to figure out the url syntax for invoking the services. For example, for invoking DLFolderService.getFolder(311) I have to send an xmlhttprequest to http://localhost:8080/c/portal/json_service?serviceClassName=com.liferay.portlet.documentlibrary.service.http.DLFolderServiceJSON&serviceMethodName=getFolder&serviceParameters=311

in the case of methods that accept multiple parameters, they must be passed comma separated.

Question 1: Is this correct?

Question 2: in the case of String parameters, how can I pass a string that contains a comma character?

Question 3: I see jsonrpc-java (jsonrpc.jar) is used for implementing this kind of web service access. Where is the endpoint so I can use a json JSONRpcClient object for accessing the service as explained in http://oss.metaparadigm.com/jsonrpc-svn-trunk/manual.html#javascript-client ?

thanks in advance
Jorge Ferrer
RE: REST-style JSON Web Services
September 19, 2007 12:53 PM
Answer

Jorge Ferrer

LIFERAY STAFF

Rank: Liferay Legend

Posts: 2761

Join Date: August 31, 2006

Recent Posts

Hi Sebastián,

I'm also a newbie on this, but as nobody has answered in a few days, I'll try to help.

I would recommend you to take a look at the service.js file. It contains a set of js APIs to access the JSON services and abstract the details that you are going to find out. If your client uses JavaScript that will be much easier. If not, you can still look at the code to learn how to build something similar for yourself. In particular to learn how the parameters are joined together take a look at the following code:

 1    getParameters: function(params) {
 2        var serviceParameters = "";
 3
 4        for (var key in params) {
 5            if ((key != "serviceClassName") && (key != "serviceMethodName")) {
 6                serviceParameters += key + ",";
 7            }
 8        }
 9
10        if (Liferay.Util.endsWith(serviceParameters, ",")) {
11            serviceParameters = serviceParameters.substring(0, serviceParameters.length - 1);
12        }
13
14        return serviceParameters;
15    }
Alex Wallace
RE: REST-style JSON Web Services
April 22, 2008 3:11 PM
Answer

Alex Wallace

Rank: Liferay Master

Posts: 640

Join Date: November 4, 2007

Recent Posts

Is it possible to authenticate in the url, kind of how it is done when using SOAP / axis ? I tried simple auth but that doesn't cut it... Only if i login in the portal can i use the REST / Json style web services... Is that the only way to do it?


UPS this is legacy... sorry about the post!
Sebastián Gurin
RE: REST-style JSON Web Services
April 24, 2008 10:30 AM
Answer

Sebastián Gurin

Rank: Junior Member

Posts: 75

Join Date: June 13, 2007

Recent Posts

accesing liferay api via rest

shows a complete test case of how to access liferay services via rest/json. hope it is useful.
Dave Morris
RE: REST-style JSON Web Services
September 16, 2009 10:40 PM
Answer

Dave Morris

Rank: Junior Member

Posts: 27

Join Date: May 7, 2009

Recent Posts

Here is an updated example that will work with Liferay 5.2.3


Original REST JSON API Blog Post

This example uses the Zend API just because it was what I had available at the time, but the concept applies to any Http client API in PHP or any other language for that matter. The main thing to notice here is that the serviceClassName is different, because ServiceBuilder no longer generates the *ServiceJSON.java files.

Also, make sure you add this line to your portal-ext.properties file to allow the json servlet to be accessible from some IP address or hostname:

json.servlet.hosts.allowed=localhost,127.0.0.1

 1
 2<?php
 3require_once ('Zend/Http/Client.php');
 4require_once ('Zend/Json.php');
 5
 6$liferay = new Zend_Http_Client();
 7
 8$liferay->setUri('http://localhost:8080/tunnel-web/secure/json');
 9
10// set parameters to return a list of countries from the Liferay db
11$liferay->setParameterGet(array(
12    'serviceClassName' => 'com.liferay.portal.service.CountryServiceUtil',
13    'serviceMethodName' => 'getCountries'
14));
15
16// set authentication
17$liferay->setAuth('userid', 'password');
18
19// send the request
20$result = $liferay->request("GET");
21
22// get back a json response
23$json = Zend_Json::decode($result->getBody());
24
25// print the json object
26echo $result->getBody();
27
28?>


And if you're having trouble getting it to work, check out SecureFilter.java, JSONServiceAction.java, and JSONServlet.java to figure out what's going on.
Corné Aussems
RE: REST-style JSON Web Services
October 27, 2011 12:10 AM
Answer

Corné Aussems

Rank: Liferay Legend

Posts: 1271

Join Date: October 3, 2006

Recent Posts

Rami Grossman:
Hi,
I'm also a newbie. The link doesn't work. Does anyone know of a working wiki on this subject?

Thanks!


For future references:
http://www.liferay.com/es/web/raymond.auge/blog/-/blogs/476431