Back-end with Google App Engine and Java

Ajeet Meena
May 29, 2018 · 5 min read

Hello, everyone. I am Ajeet Kumar Meena and I am the founder and CEO of WallMag. I have been working with Google app-engine from last one and half year. I started learning app-engine for my start-up WallMag. The reason I am writing this tutorial is that I found a lot of friends working on front-end technologies and wants to learn back-end. I choose app-engine because of its simplicity and scalability. I will be covering this article in such a way that you alone would be able to write and deploy your own complicated back-end from scratch after completing the whole series. After this series, you would be able to think your application in a such a manner that it would be both costs effective and optimized. Throughout this series, I will be giving my own perspective of thinking while writing an application which will also be covering some code practices. You can use your own techniques and strategies to design your own applications.


Table of Index

  1. Getting Started: Install the required software and create your first API and deploy your application to local. Use API explorer to test and run your APIs.
  2. Deploy to GCP: Deploy your first application to Google Cloud Platform. Some introduction to GCP.
  3. Datastore: Use Google NoSQL database engine with objectify. Write few queries and get around with some objectify concepts.
  4. Cost effective Architecture Design: Think your app in Google datastore manner to optimize queries and cost of your application. Introduction to mem-cache and session-cache.
  5. Cloud Storage: Use Google cloud storage (GCS) to upload and download files
  6. Authentication: Authenticate your APIs with firebase. Some introduction to firebase
  7. Common APIs: Write common logics for filtering data. Data syncing with deployment. Some common APIs which can fit any application
  8. Extras: Use Google Image Services, Google Search and Machine Learning APIs.

Getting Started

Google App Engine (GAE) is a web framework and cloud computing platform for developing and hosting web applications in Google-managed data centres. The main advantage that I see with GAE is its learning curve, free operations and scalability. With proper practices, one might reduce their server bills from lacks to almost thousands. Also by using GAE as the main back-end, you would be able to use other GCP offerings with almost zero latency.

Throughout this series I will be using Android Studio as the primary IDE . One can use eclipse and IntelliJ also (We recently shifted to IntelliJ which I will cover on next series). There is not much of a difference in both but yes Android Studio provides a rice user interface and gradle.

Installing App Engine in Eclipse

  1. Download Eclipse
  2. Download Google Cloud SDK for your OS
  3. Install app-engine package from Google Cloud SDK by running: gcloud components install app-engine-java
  4. Install Eclipse plug-in from help>marketplace> Search google cloud

Ref: https://cloud.google.com/eclipse/docs/quickstart

Installing App Engine in Android Studio

There is nothing much to-do. Make sure you have downloaded GCP SDK from above 2nd Step.

Create your first Endpoint

First we will create a new app engine project which by default come with an example Endpoint.

Eclipse: https://cloud.google.com/eclipse/docs/creating-new-webapp

Android Studio: Select the top most level of your project, right click New > Module and select App Engine Java Endpoints

Congrats! you have created your first Endpoint. You can now run your code to check your API in localhost. Just click run or debug to debug in your IDE. Wait a second, Endpoint?

Google Cloud Endpoints is a distributed API management system. It provides an API console, hosting, logging, monitoring and other features to help you create, share, maintain, and secure your APIs. Well for more, read this. In app-engine you define various Endpoint which contains many APIs. For instance consider Upload Endpoint which might contain image upload API and video upload API. More example? User Endpoint which would be dealing with user operations. Endpoint make APIs more manageable and accessible.

Lets create a User Endpoint class and check out it’s different element.

@Api(
name = "userAPI",
version = "v1",
clientIds = {Constants.ANDROID_CLIENT_ID},
audiences = {Constants.ANDROID_AUDIENCE})
)
public class UserEndpoint{
// User Endpoint API methods}

We define a class as Endpoint by defining @Api annotation. It contains following attributes:

  1. name: Name of the Endpoint. Also forms the base URL.
  2. version: Version of the User Endpoint. Version name also forms the base url.
  3. clientIds: Client ids which are allowed to access this endpoint. We will discuss this more in Authentication.
  4. audiences: Audiences which are allowed to access this Endpoint. i.e Android, iOS or Web.

As we can see how easy it is manage authentication, version of an Endpoint. Let’s create an insert user API inside UserEndpoint class. P.

// This method is inside UserEndpoint class@ApiMethod(
name = "insert",
path = "user",
httpMethod = ApiMethod.HttpMethod.POST)
public User insert(User user, @Nullable @Named("isAdmin") Boolean isAdmin) throws UserNotFoundException{
User = new User();
// User insert code
return user;
}

By defining @ApiMethod on a method will make it an API.

  1. name: Name of the API.
  2. path: Path of the API which will form the url.
  3. httpMethod: Http Method to access this API. i.e POST, GET, PUT, DELETE
  4. clientIds: Same as @API annotation but it will override Endpoint clientIds
  5. audiences: Same as @API annotation but it will override Endpoint audiences

In the above API method “user” parameter is the request body and isAdmin is request parameter which could be null.

NOTE : An API method can contain only single Request body. Also, It cannot be a Collection or a Map. One can make a wrapper object to handle Collection and Map. Also an parameter cannot be an custom object.

PRACTICE: Don’t create a wrapper object for API response to give a status of an API error. Prefer throwing an exception in a case of an error from the method only. Doing this will help writing the better test cases and also helps client about the encountered error. We will follow this practice in later sections.

More information on annotation available here.

API Explorer

API explorer is a handy tool to test and verify your APIs. Generally an industrial application contains 100s of APIs. To overcome this Google has made API explorer. To access api explorer just hit

http://apis-explorer.appspot.com/apis-explorer/?base=http://localhost:8080/_ah/api#p/

Understanding API explorer I am leaving up-to you as it is not that difficult.

That’s it folks. In next section we will create a account in GCP and we will deploy our application in Google servers.

Please leave comment and share. Thanks. :)

Ajeet Meena

Written by

Entrepreneur | Musician | Founder wallmag.io

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade