Building of Java API
In this part of the tutorial, we'll use the App Engine Maven artifact
Next, let's build a more complete backend API wiith GET and POST methods that write to and fetch from the datastore.
Creating a Simple Hello World Backend API
In this part of the tutorial, we'll use the App Engine Maven artifact
endpoints-skeleton-archetype
to create a new
Endpoints project. We'll then show you how to add code.Creating and building the project
To create the Hello World backend API:-
In a terminal window, change to the directory where you want to build the project.
-
Invoke the following Maven command:
mvn archetype:generate -Dappengine-version=1.9.23 -Dfilter=com.google.appengine.archetypes:
-
Enter the number
2
to select the archetyperemote -> com.google.appengine.archetypes:endpoints-skeleton-archetype
from the list of App Engine archetypes.
-
Accept the default to use the most recent version.
-
When prompted to
Define value for property 'groupId'
, enter the namespacecom.example.helloworld
to keep this tutorial in sync with the source files at GitHub. (The typical convention is to use a namespace starting with the reversed form of your domain name.)
-
When prompted to
Define value for property 'artifactId'
, supply the project namehelloworld
.
-
When prompted to
Define value for property 'version'
, accept the default value.
-
When prompted to
Define value for property 'package'
, accept the default value.
-
When prompted to confirm your choices, accept the default value (
Y
).
-
Wait for the project to finish generating, then take a look at the resulting
project layout:
Thepom.xml
file contains all the configuration and dependencies needed for your backend API.
-
Change directories to the project's Java source directory:
/src/main/java/com/example/helloworld
.
-
Create a JavaBean
class file named
MyBean.java
copy-paste the following code into it
package com.example.helloworld; /** The object model for the data we are sending through endpoints */ public class MyBean { private String myData; public String getData() { return myData; } public void setData(String data) { myData = data; } }
- requires a browser that supports JavaScript and iframes.]</p>
We'll use this JavaBean object to send data through the backend API.
-
Edit the file
YourFirstAPI.java
, replacing all of the contents with the following code:
package com.example.helloworld; import com.google.api.server.spi.config.Api; import com.google.api.server.spi.config.ApiMethod; import com.google.api.server.spi.config.ApiNamespace; import javax.inject.Named; /** An endpoint class we are exposing */ @Api(name = "myApi", version = "v1", namespace = @ApiNamespace(ownerDomain = "helloworld.example.com", ownerName = "helloworld.example.com", packagePath="")) public class YourFirstAPI { /** A simple endpoint method that takes a name and says Hi back */ @ApiMethod(name = "sayHi") public MyBean sayHi(@Named("name") String name) { MyBean response = new MyBean(); response.setData("Hi, " + name); return response; } }
@Api; this is where we set the configuration of the backend API. - Notice the @ApiMethod
annotation; it allows you to configure your methods.
Finally, notice the use of thejavax.inject.Named
annotation (@Named
) in thesayHi
method definition. You must supply this annotation for all parameters passed to server-side methods, unless the parameter is an entity type.
-
Build the project by invoking
Wait for the project to build. When the project successfully finishes, you will see a message similar to this one:mvn clean install
[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------ [INFO] Total time: 14.846s [INFO] Finished at: Tue Jun 03 09:43:09 PDT 2014 [INFO] Final Memory: 24M/331M
Running and testing the API locally
To test the backend API locally:-
From the main directory
helloworld/
, start the API in the development server as follows:
Wait for the dev server to start up. When the server is ready, you will see a message similar to this one:mvn appengine:devserver
[INFO] INFO: Module instance default is running at http://localhost:8080/ [INFO] Jun 03, 2014 9:44:47 AM com.google.appengine.tools.development.AbstractModule startup[INFO] INFO: The admin console is running at http://localhost:8080/_ah/admin [INFO] Jun 03, 2014 9:44:47 AM com.google.appengine.tools.development.DevAppServerImpl doStart[INFO] INFO: Dev App Server is now running
-
Visit the APIs Explorer at this URL:
(The Google APIs Explorer is a built-in tool that lets you send requests to a backend service without using a client app.)http://localhost:8080/_ah/api/explorer
-
Click myApi API to display the methods
available from this API.
-
Click myApi.sayHi to display the Explorer for this method:
-
In the name field, supply your name, and click
Execute and check the Response for the greeting.
Congratulations!
You've just built and tested your first backend API!Next, let's build a more complete backend API wiith GET and POST methods that write to and fetch from the datastore.