Hosted API services?

Hi,

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.

Thanks!
-Ian

Hi Ian,

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.

Does it help?

Regards,
Mark

Hi Mark,

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 ) );
    }
}

Hi Ian,

Have you tried sending it just “fred” ?

Regards,
Mark

Mark,

Yes, sending fred (without quotes) yields:
{
“code”: 8002,
“message”: “Could not parse request with message: Invalid Json”
}

sending “fred” (with quotes) yields
{
“code”: 14004,
“message”: “Service invocation failed: weborb.exceptions.ServiceException: com.backendless.exceptions.BackendlessException: weborb.reader.NullType cannot be cast to weborb.reader.NamedObjectServiceException”
}

Thanks,
-Ian

Hi, Ian,

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!

Best regards, Artur.

Regarding to your problem: if you method accepts string try putting string as json string delimited with double-quotation : “String”.

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”
}

sending “fred” (with quotes) yields
{
“code”: 14004,
“message”: “Service invocation failed: weborb.exceptions.ServiceException: com.backendless.exceptions.BackendlessException: weborb.reader.NullType cannot be cast to weborb.reader.NamedObjectServiceException”
}

Ian,

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:

Service and signature:
http://support.backendless.com/public/attachments/fdfc05f96b0e79ac29f3f83c2cf59ae4.jpg</img>

Service invocation:
http://support.backendless.com/public/attachments/86541378ca6a505eea80b36480c9ac5a.jpg</img>

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.

Thanks,
-Ian

Hi Ian,

Yes, you can use all Backendless APIs from the Hosted services. I looked closer at your code and see the problem right here:

String whereClause = "username=" + userName;

The problem is that the value in userName must be enclosed into single quotes:

String whereClause = "username='" + userName +"'";

Please give it a try and let us know if it works.

Btw, it is always a good idea to test your “where clause” queries using Backendless console. Here’s how you can do it:
https://backendless.com/feature-14-sql-based-search-for-data-objects-using-console/

Regards,
Mark

NEVERMIND! :slight_smile:
It would help if I wrapped the variable I was concatenating to the where clause in single quotes:

String whereClause = "username='" + userName + "'";
// vs.
String whereClause = "username=" + userName; // this was causing the error.

Thanks so much for the help!
-Ian

Looks like we noticed the problem at almost exactly the same time.
Cheers, and thanks again for the help.
-Ian