Creating the Hello Endpoints Complete Sample Backend API
Hello Endpoints Description
Creating the project
Running and testing the API locally
Deploying to and running on App Engine
In this part of the tutorial, we'll use the App Engine Maven artifact hello-endpoints-archetype to create a complete sample backend API.
Hello Endpoints Description
The Hello Endpoints sample backend API supports:
A simple GET method that retrieves either of two canned responses based on user choice.
A simple GET method that retrieves all of the canned responses.
A POST method that provides a user-supplied greeting and multiplier to the backend API which then returns the greeting repeated the number of times specified by the multiplier.
An OAuth protected method requiring user sign-in, which returns the user's email address.
The UI provided by the included sample web client looks like this:

Creating the project
To create the HelloEndpoints 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.34 -Dfilter=com.google.appengine.archetypes:Enter the number
4to select theremote -> com.google.appengine.archetypes:hello-endpoints-archetypefrom the list of App Engine archetypes.Select the most recent version from the displayed list of available archetype versions by accepting the default.
When prompted to
Define value for property 'groupId', you can supply a namespace of your choosing, but if you want to keep this tutorial in sync with the source files on GitHub, usecom.example.helloendpoints. (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 namehelloendpoints.Accept the defaults for the remaining prompts.
Wait for the project to finish generating, then take a look at the resulting project layout:

The layout looks very similar to the the layout already described in Creating a Simple Hello World Backend API. The main difference is that the backend API is already defined in the the Java source.
HelloGreetings.javais the JavaBean class used for the responses from the backend API andGreetings.javacontains the backend API definition. We'll describe the code inGreetings.javalater in the code walkthrough.Also, note the complete web client defined in
src/main/webapp/js/base.jsandsrc/main/webapp/index.html, along with the related CSS files. We won't go into this web client in this tutorial, but you can find a full description of the web client in the web client tutorial.Edit the file
src/main/webapp/WEB-INF/appengine-web.xmlto set<application>to the project ID you obtained earlier during Setup. (Or, look up the Client ID for web application in the Google Cloud Platform Console.)Edit the file
src/main/java/com/example/helloendpoints/Constants.javaas follows:Set
WEB_CLIENT_IDto the web client ID you obtained earlier during Setup. (Or, look up the web client ID in the Google Cloud Platform Console.)Add the following line to the
Constantsclass:public static final String API_EXPLORER_CLIENT_ID = com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID;This line is needed for testing of OAuth protected methods in the APIs Explorer.
Edit the file
src/main/java/com/example/helloendpoints/Greetings.javato addConstants.API_EXPLORER_CLIENT_IDto the list ofclientIDsin the@Apiannotation.Edit the file
src/main/webapp/js/base.jsto setgoogle.devrel.samples.hello.CLIENT_IDto the web client ID you obtained earlier during Setup.Build the project by invoking
mvn clean installWait for the project to build. When the project successfully finishes, you will see a message similar to this one:
[INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 17.169s [INFO] Finished at: Thu Jun 05 13:56:32 PDT 2014 [INFO] Final Memory: 23M/234M
Running and testing the API locally
To test the backend API locally:
Start a new Chrome session as described in How do I use Explorer with a local HTTP API and specify
8080as thelocalhostport (localhost:8080).From the main directory
helloendpoints/, start the API in the development server as follows:mvn appengine:devserverWait for the dev server to start up. When the server is ready, you will see a message similar to this one:
[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 runningVisit the backend API via the built-in APIs Explorer at this URL:
http://localhost:8080/_ah/api/explorerAlternatively visit the web client at
http://localhost:8080.Note: If you see a red warning banner warning about using HTTP, which is insecure, locate the shield icon (which, in Chrome, is located in the address bar), and click it, then click Load unsafe scripts to allow APIs Explorer to run. None of these HTTPS issues apply since you are running the APIs Explorer locally in a test environment.
To send requests to the backend API, visit the APIs Explorer in your browser at this URL:
http://localhost:8080/_ah/api/explorerThis opens up the APIs Explorer for your backend. Scroll down if necessary to see the list of APIs displayed with helloworld API in that list.
Click helloworld API to display the available methods. Notice how the name specified in your
@Apiannotation (helloworld) is prepended to the API class and method name.Click helloworld.greetings.getGreetings to display the Explorer for it:

Our simple backend has two stored messages in an array. Get the first message by entering a value of
0in the Id text box, then click Execute without OAuth.Notice the display of the request and response. In the Response, you'll see the OK result and the returned message, hello world!.
Enter a value of
1in the Id text box, then click Authorize and execute; this time the message is goodbye world!.Try out the other methods. The
listGreetingmethod returns the stored list of greetings,multiplyrepeats a user-posted greeting the specified number of times, andauthedis an OAuth protected method.To try the "authed" method:
Click helloworld.greetings.authed.
Click Authorize and execute.
When prompted to supply additional scopes, use the default scope provided by clicking Authorize and execute again.
Observe the results.
Deploying to and running on App Engine
After you finish testing, you can deploy to App Engine. To deploy to App Engine:
Make sure you are logged into the Google account you used to create the Google Cloud Platform Console project.
Open the OAuth 2.0 client ID form for your project in the Cloud Platform Console.
If you see the Configure consent screen button at the bottom of the form, click it, supply a Product name, then click Save.
Fill out the form:
Select Web application as the Application Type.
If you are testing the backend locally, specify
http://localhost:8080in the textbox labeled Authorized JavaScript origins. If you are deploying your backend API to production App Engine, specify the App Engine URL of your backend API in the textbox labeled Authorized JavaScript origins; for example,https://your_project_id.appspot.com, replacingyour_project_idwith your actual App Engine project ID.Important: You must specify the site or host name under Authorized JavaScript origins using
httpsfor this to work with Endpoints, unless you are testing withlocalhost, in which case you must usehttp.Click Create.
You might want to keep this tab open so you can look up the client and project IDs later.
From the main project directory,
helloendpoints/, invoke the commandmvn appengine:updateFollow the prompts: when you are presented with a browser window containing a code, copy it to the terminal window.
Wait for the upload to finish, then visit the URL you specified above for the Authorized Javascript Origins (
https://your_project_id.appspot.com).If you don't see the web client app, or if the client doesn't behave as expected, check for a successful deployment.
Congratulations!
You've just built and deployed a backend API and a web client to production App Engine!
We haven't looked at the backend API code yet. So let's walk through the code next to get an understanding of how a backend API is implemented.