Message:

Subscribe rss
Blog categories
All | Backendless features
Easy Web Services with Backendless
June 11, 2019 by Sergey Chupov

Easy Web Services with Backendless

Have you ever wondered why is it often so tedious so make your simple Java app a web server, with the methods becoming the endpoints? You need to add libraries, write additional “web” wrappers, set up a server and a hosting, configure load balancing and much, much more. Do you really have to go through all these things when you just want this piece of code be available via the Web so that others can invoke it and get some fancy results?

The good news is that nowadays this is much less of a problem: with services like Backendless, it is easy to transform your custom code into a real REST service available via both REST and JS/iOS/Android/Java APIs. If needed, authentication also comes available out-of-the-box. It is worth noting that this is all for no charge except for the number of API services you may have and the number of API calls you’ll make to the service in a month.

In this article, we’ll show you how to make your HelloWorld app really say Hello to the whole World.

Step 1: Create an application in Backendless

In order to take advantage of all the Backendless functionality, you need to register and create an application first. To do this, go to https://develop.backendless.com and either create an account by clicking the “Register” link or login via Facebook or Google. This should be pretty straightforward considering you’ve already registered at least somewhere.

Upon registration and logging in, you’ll be prompted to create an application:

New application creation form

An application (or just app for short) in Backendless is itself your whole backend, including hosting, database, file storage, user identity manager and much more. These components are called Backendless Services, e.g. Data Service is a database, User Service is user management etc. API services is also one of Backendless Services which we’ll discuss in detail later.

On this form, you choose a name for your application. In case you’re developing some kind of a mobile app, the name of your backend application could be the same as the name of your mobile app. Otherwise, you can pick any name you like, including dummy names like “app1” or “Testing”. In case you were referred by someone who already uses Backendless, they could send you a referral code, so that you both get a discount when you graduate to a paid plan and make any extension purchases. If you didn’t receive a referral code, just leave the field blank.

After choosing your application name and clicking the “Create” button, you’ll be presented with the Backendless Console dashboard:

Your application’s console, the home page

Backendless Console is the administration panel for your backend/application. Here you put in the configurations for the services (if required), user registration and login settings, you can view and edit the data stored in your backend, generate client code for your backend (we’ll cover this further, too), create your API services and much more. We’re currently interested in the last one, so let’s click on the “Business Logic” tab on the left menu and see the API services management panel:

The Business Logic tab on Backendless Console

As you see, we don’t have any services yet. At this time we should go to our IDE or text editor and sketch a simple Java class.

Step 2: Prepare the API service code

For demo purposes, let’s have a simple class with a method to determine whether a given number is prime or not:

(Don’t forget to put this PrimeService.java  file into com/primeservice  folder since it’s in the  com.primeservice  package.)

Now, in order to make it recognizable as an API service for Backendless, all you need to do is either make it implement IBackendlessService  interface:

or mark it with BackendlessService  annotation:

In order to be able to upload this code to Backendless, we need to compile it and package into jar. This is accomplished via two simple commands from the root directory:

A few things to note here:

  • The Backendless Java/Android Client Library (SDK) is specified in the classpath, since we use either an annotation or an interface from the com.backendless  package; this library can be downloaded with this link from Maven Central repository;
  • we use the  -g:vars  parameter in order to preserve method parameter names in the compiled .class file; this is desirable to have meaningful parameters displayed on the console instead of auto-generated argN values;

Now we should have a PrimeService.jar  archive ready to be deployed to Backendless and already act as a web-service!

Step 3: Deploy your code as an API service

Let’s return to where we finished Step 1, namely the Business Logic tab in Backendless Console. To deploy your jar archive, click the text link “Add one” in the middle, then click the “Browse” button and choose the archive. Here’s what you should see now:

Uploading a JAR API service to Backendless

Click the “Save” button and your jar-packaged API service will be uploaded to your app’s file storage and parsed to process HTTP requests to your isPrime  method:

Deployed API service panel

To try it out, put in some test value and click “Invoke” button on the right:

A response from the deployed service

You get a 200 OK response and a response value true  right from your service method. Clicking on the “Invoke” button triggered a REST request to the endpoint which you can see by clicking on “show full URL”. Also, you can reproduce this request with the generated curl command at the very same screen:

Auto-generated curl command

And finally you can take advantage of the complete auto-generated “SDK”s for your API service, downloadable by a click on the icon near the deployed service name:

Client code generation for your service

Remember everything you needed to do to get all this is to add one @BackendlessService  annotation? :)

Step 4: User Authentication

Suppose you only want authenticated users to be able to invoke the endpoint. Fortunately, since you have access to the whole platform immediately, the user management system is here as well. This means the users of your app can already register and login to your app using the built-in User Service API.

Now allowing or denying the service invocations to specific users or roles is just a matter of configuration. To do that for your service, click on the “lock” icon on the panel above the services list:

The “lock” icon leading to permissions configuration

Set up the proper permissions by switching the appropriate checkmarks:

Permissions configuration for the service

Assuming we’ve done everything correct, here’s the response the unauthorized users now receive:

Response when permission for invocation is denied

Summary

Every new technology aimed at simplifying our developers’ lives usually comes with a certain barrier, meaning that we should first become acquainted with it and go through some amount of learning. Using a service like Backendless may feel that way at first, but this is largely the result of the platform’s wide array of capabilities. Here we tried to elaborate on each step you need to do to accomplish a pretty mundane but sometimes large task: to make your Java methods available as REST endpoints to a wide range of clients, from curl to JavaScript and Swift. There’s no doubt that as you progress through the learning curve, your development will become much more swift and painless. So just go on and try it! And in case you’ve already followed the guide with me, go on and create an awesome app you’ve always wanted to share with the world! 😉

Share this post
Tweet about this on TwitterShare on FacebookGoogle+