Is there any documentation on how to use/build Hosted API services? I couldn’t find anything in the documentation directly addressing this feature and was curious how I could leverage it. I added the sample “Shopping Cart” service as a hosted API service to my current application, but sadly, it wasn’t clear what exactly I needed to do to leverage this.
It is very straight-forward. You upload a jar file with your compiled code. The jar must contain at least one service - an arbitrary Java class which implements the IBackenlessService interface. The interface has no methods, simply a marker.
Once a jar is uploaded, Backendless turns the code into a programmable service with its own APIs.
We automatically generate client side libraries which contain the APIs to interact with your code. You will see all those links (Android, iOS, JavaScript, etc). This is where you can download client-side libraries for the service.
The console also includes the API inspector - a visual interface to send REST requests to the service and invoke its methods.
Thanks! I got this working and am currently experimenting with this.
I have one method in this class which is currently responding with:
{
“code”: 14004,
“message”: “Service invocation failed: RuntimeException: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token\n at [Source: N/A; line: -1, column: -1], cause Can not deserialize instance of java.lang.String out of START_OBJECT token\n at [Source: N/A; line: -1, column: -1]”
}
here’s the class for the service I’m experimenting with… perhaps it’s something obvious in my code?
My request body looks like this, when testing from the API console:
{“userName”:“fred”}
I’ve also tried just sending several variants, escaping and unescaping quotes and curly braces, etc…
public class DgtzUserService implements IBackendlessService
{
public Boolean validateUsername( String userName )
{
//
// constrain query
//
String whereClause = "username=" + userName;
BackendlessDataQuery userQuery = new BackendlessDataQuery();
userQuery.setWhereClause( whereClause );
//
// get query result
//
BackendlessCollection<BackendlessUser> queryResult = Backendless.Persistence.of( BackendlessUser.class ).find( userQuery );
//
// if we got query results, then the
// display name isn't available.
//
return ( !( queryResult.getTotalObjects() > 0 ) );
}
}
We are glad to hear you are interested in Hosted API Services. However before you start we need to inform you that this is an experimental feature. Some features are still unavailable, for instance debug, concurrency etc. These features will be introduced in following releases.
Just for your convenience I post here some suggestions and workarounds you may use:
To build complex jar you can use Surefire maven plugin.
To make log available you need to execute this code:
LogBuffer.getInstance().setLogReportingPolicy( 1, 0 )
You can not have cyclic relations in method return types of IBackenlessService
You may have only one class marked by IBackenlessService
Of course if you will have any questions or suggestions how can we improve our Hosted Services, contact us!
Artur,
Thanks for responding. I’ve tried sending my argument through the API console wrapped in quotation marks and without and I get different exceptions, described in my last comment above.
sending fred (without quotes) yields:
{
“code”: 8002,
“message”: “Could not parse request with message: Invalid Json”
}
I was not able to reproduce the problem. I created a service with an identical signature as yours and can invoke it without a problem. Please take a look:
Mark,
Thanks for your response, I am able to replicate your successful invocation of that service method, but it appears that the error is being thrown by my call to Backendless.Persistence.of( BackendlessUser.class ) as I can replicate throwing this error when this call is in the method.
Is it possible to get data from Backendless.Persistence or Backendless.Data within a hosted API? I noticed in your demo video that you were only accessing objects from Backendless.Cache.