Foros de discusión

JSON web services default parameters

Julien Tabouret, modificado hace 8 años.

JSON web services default parameters

Junior Member Mensajes: 27 Fecha de incorporación: 17/05/13 Mensajes recientes
Hello everyone,

I am currently writing a search JSON web service based on Lucene search. And to build the SearchContext, I need to provide the companyId.

Reading the documentation, I found out that company-id is defined as a default parameter of JSON web services. But when I call my web service from the /api/jsonws form or through curl, leaving the company-id blank, I get the following error :
"Unable to convert value: ; <--- java.lang.NumberFormatException: For input string: \"\""

The signature of my remote service is as follow :
List<myentity> search(int nbResults, int page, long companyId)</myentity>

And here is the URL I called :
http://localhost:8080/api/jsonws/my-portlet.my-entity/search/nb-results/20/page/1/-company-id

I also tried not to provide the company-id parameter, but the result is not good either :
"exception":"No JSON web service action associated with path /my-entity/search and method POST for //my-portlet"

So how is this default parameters supposed to work ?
Thank you in advance for your advices.

Best regards.
thumbnail
Denis Signoretto, modificado hace 8 años.

RE: JSON web services default parameters

Expert Mensajes: 375 Fecha de incorporación: 21/04/09 Mensajes recientes
Hi Julien,

(at the moment I'm writing) the link to the documentation reports:
Julien Tabouret:
When accessing secure JSON web services (i.e., the user has to be authenticated), some parameters are made available to the web services by default. Unless you want to change their values to something other than their defaults, you don’t have to specify them explicitly.
Here are the default parameters:
- userId: The id of authenticated user
- user: The full User object
- companyId: The users company
- serviceContext: The empty service context object


I think documentation it's bit ambiguous. Initially I understand (like you) that if your service explicitly declares parameters named "userId", "user", "companyId" or "serviceContext" and you don't pass it, jsonws service will fit the values for you.
After same trials (with same results like yours) my understanding is that within a service implementation, for authenticated users, if you don't need to specify them explicitlyyou you can use the following API:

getGuestOrUserId()
getGuestOrUser()
getPermissionChecker()
...


HTH

Bye,
Denis.
Julien Tabouret, modificado hace 8 años.

RE: JSON web services default parameters

Junior Member Mensajes: 27 Fecha de incorporación: 17/05/13 Mensajes recientes
Hello Denis,

I came to the same conclusion.
So only the current user (or guest) is available through getGuestOrUser(). The rest (company, serviceContext), you have to get it by yourself.

This is not a problem in itself.
But to be mislead by the official document I honestly think it is a major problem. And unfortunately it happens often.
thumbnail
Denis Signoretto, modificado hace 8 años.

RE: JSON web services default parameters

Expert Mensajes: 375 Fecha de incorporación: 21/04/09 Mensajes recientes
Hi Julien,

Yes I agree, I think documentation for this particular case need to be fixed.
I hope to get some clarification by someone of Liferay staff.

For companyId and serviceContext parameters you can get them in this way:
long companyId = CompanyThreadLocal.getCompanyId();
ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();


Bye,
Denis.