Foros de discusión

RE: REST-style JSON Web Services

thumbnail
Sebastián Gurin, modificado hace 16 años.

REST-style JSON Web Services

Junior Member Mensajes: 75 Fecha de incorporación: 13/06/07 Mensajes recientes
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
thumbnail
Jorge Ferrer, modificado hace 16 años.

RE: REST-style JSON Web Services

Liferay Legend Mensajes: 2871 Fecha de incorporación: 31/08/06 Mensajes recientes
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:

	getParameters: function(params) {
		var serviceParameters = "";

		for (var key in params) {
			if ((key != "serviceClassName") && (key != "serviceMethodName")) {
				serviceParameters += key + ",";
			}
		}

		if (Liferay.Util.endsWith(serviceParameters, ",")) {
			serviceParameters = serviceParameters.substring(0, serviceParameters.length - 1);
		}

		return serviceParameters;
	}
thumbnail
Alex Wallace, modificado hace 16 años.

RE: REST-style JSON Web Services

Liferay Master Mensajes: 640 Fecha de incorporación: 5/11/07 Mensajes recientes
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!
thumbnail
Sebastián Gurin, modificado hace 15 años.

RE: REST-style JSON Web Services

Junior Member Mensajes: 75 Fecha de incorporación: 13/06/07 Mensajes recientes
accesing liferay api via rest

shows a complete test case of how to access liferay services via rest/json. hope it is useful.
thumbnail
Rami Grossman, modificado hace 15 años.

RE: REST-style JSON Web Services

New Member Mensajes: 18 Fecha de incorporación: 9/09/08 Mensajes recientes
Hi,
I'm also a newbie. The link doesn't work. Does anyone know of a working wiki on this subject?

Thanks!
thumbnail
Jorge Ferrer, modificado hace 15 años.

RE: REST-style JSON Web Services

Liferay Legend Mensajes: 2871 Fecha de incorporación: 31/08/06 Mensajes recientes
Hi Rami,

Here is the correct link: Json Services

Could you create a wiki article based on that blog entry?
thumbnail
Dave Morris, modificado hace 14 años.

RE: REST-style JSON Web Services

Junior Member Mensajes: 27 Fecha de incorporación: 7/05/09 Mensajes recientes
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


<!--?php
require_once ('Zend/Http/Client.php');
require_once ('Zend/Json.php');

$liferay = new Zend_Http_Client();

$liferay--->setUri('http://localhost:8080/tunnel-web/secure/json');

// set parameters to return a list of countries from the Liferay db
$liferay-&gt;setParameterGet(array(
    'serviceClassName' =&gt; 'com.liferay.portal.service.CountryServiceUtil',
    'serviceMethodName' =&gt; 'getCountries'
));

// set authentication
$liferay-&gt;setAuth('userid', 'password');

// send the request
$result = $liferay-&gt;request("GET");

// get back a json response
$json = Zend_Json::decode($result-&gt;getBody());

// print the json object
echo $result-&gt;getBody();

?&gt;


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.
thumbnail
Corné Aussems, modificado hace 12 años.

RE: REST-style JSON Web Services

Liferay Legend Mensajes: 1313 Fecha de incorporación: 3/10/06 Mensajes recientes
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