We continue with the tutorial about using Google Cloud Endpoints. As introduced in the first part, we plan to teach you how to construct a simple web backend storing data and then expose it by means of a public REST-based API. We explained in the first post of the tutorial how to set up the environment. Now it’s time to explain how to create the core application. Finally, in the third post in the series we will explain how to interact with our application.
Now we have Eclipse and the latest version of the Google Plugin for Eclipse installed. Let’s create a new Web Application Project by clicking the Google icon and selecting New Web Application Project.
The New Web Application Project dialog appears, and we have to set it up as shown in the next screenshot. Basically, set the project name and the package where to include the source Java classes. Now click Finish and then the new project appears in the Eclipse project explorer view.
Then, since we are going to build an app that deals and stores posts, the first thing we are going to need create a Post class. To do that, right click on the project package, select New and then Class. Call it Post and then follow Eclipse instructions to create it.
Next screenshot shows the contents of Posts.java. It is a standard Java class for storing information about a post with special annotations for AppEngine. In our app, a post contains String fields to store a title, the post content, and the author. Also, we include a Key attribute as the primary key (see the @PrimaryKey annotation above the Key field definition) of the Post entity when stored in AppEngine’s datastore. The other fields are marked with the @Persistent annotation to indicate the their values must also be stored in the datastore. Following the Post fields we create the setter and getter methods to access them.
Once we have the Post class defined, next step is the magic one. Just right click on the Post class name in the Project Explorer and select Google, and then Generate Cloud Endpoint Class in the contextual menu that appears. That class will be the one containing the methods of our API that are accessible by external tools/users/programs to manage the Posts of our app.
The Google Plugin for Eclipse automatically creates a PostEndpoint Java template class that does the basic List, Add, Update, and Delete operations on Post objects. The code is meant to be a starter that you can modify to add your application logic. Let’s review its main characteristics (note that we have added minor changes to the generated code).
The PostEndpoint class is annotated with @Api annotation. It establishes the name of our API (we changed it to “myposts”). It is important because that name will be part of the final URL to make the calls to our app in Google’s AppEngine.
Each public method in this class that is annotated with an @ApiMethod annotation is exposed as a REST\RPC endpoint url that can be called from an external app. The default @ApiMethod annotation generated contains a name. We also indicated the path and the httpMethod. The next capture shows the method listPost which will be accessed through a HTTP GET request to a specific URL that contains the API name “myposts” and finishes with the path “/post”. The result of calling that method is a JSON object with all the posts saved in AppEngine’s datastore. The coded within the method that you can see in the screenshot is the autogenerated code without changes.
Next screenshot shows the methods to get a specific post by its identification code, and to insert a new post. Note that the method to get a post contains the id of the post as input parameter. We modified the URL path of the annotation to include the id, which is then passes as input parameter into the getPost method. The id is included within the Key field of a Post instance and is used in the getObjectById method of the Datastore PersistenceManager object. Alternatively, for the insertPost method the HTTP request needed is a POST request with a Post object (serialized in JSON format, as we will see in the third post of this tutorial series) as a parameter.
In the last part of the generated Endpoint we have the methods to update and delete a post object. The updatePost method receives a Post object as a parameter and the id of the post to update. We modified the generated code to make it more useful. Basically, we find the existing post to update and update its fields with the fields of the input parameter post.
For the case of the removePost method, it is called through a HTTP DELETE request. The method receives the id of the post that has to be deleted, finds that post and calls the deletePersistent method of the PersistenceManager object. That way the post object is removed from the datastore.
If you achieved the last part of the tutorial, you did a good job!
Stay tuned for the final part of this tutorial where we’ll show you how to use the generated Google Cloud Endpoint for posts and how to deploy it to Google AppEngine.