Note for Windows users: If you are installing the JDK and Maven on Windows, install them in a directory that doesn't have a space in the path. See Maven on Windows for more information.
You need an application to send requests to the sample API.
curl
, which typically comes pre-installed on your operating system. If you don't have curl
, you can download it from the curl
Releases and downloads page.Invoke-WebRequest
, which is supported in PowerShell 3.0 and later.gcloud auth login
gcloud auth application-default login
app-engine-java
component:
gcloud components install app-engine-java
gcloud components update
gcloud config set project YOUR_PROJECT_ID
Replace YOUR_PROJECT_ID
with your Google Cloud project ID. If you have other Google Cloud projects, and you want to use gcloud
to manage them, see Managing gcloud CLI configurations.
gcloud app regions list
YOUR_PROJECT_ID
with your Google Cloud project ID and YOUR_REGION
with the region where you want the App Engine application created in.
gcloud app create \ --project=YOUR_PROJECT_ID \ --region=YOUR_REGION
To clone the sample API from GitHub:
Clone the sample repository to your local machine:
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
Change to the directory containing the sample code:
cd java-docs-samples/appengine-java8/endpoints-v2-backend
The sample code includes the Endpoints Frameworks tool that generates an OpenAPI configuration file that describes the sample code's REST API. Follow the steps in this section to configure and build the sample Maven project so that you can then generate the OpenAPI configuration file.
Adding the project ID to the sample API codeYou must add the project ID obtained when you created your project to the sample's pom.xml
before you can deploy the code.
To add the project ID:
Edit the file java-docs-samples/appengine-java8/endpoints-v2-backend/pom.xml
.
Search for <endpoints.project.id>
, and replace YOUR_PROJECT_ID
with your Google Cloud project ID.
For example:
<endpoints.project.id>example-project</endpoints.project.id>
Save your changes.
To build the project:
Make sure you are in the directory java-docs-samples/appengine-java8/endpoints-v2-backend
.
Run the following command:
mvn clean package
Wait for the project to build. When the project successfully finishes, a message similar to this one displays:
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.846s
[INFO] Finished at: Wed April 13 09:43:09 PDT 2016
[INFO] Final Memory: 24M/331M
You use a tool from the Endpoints Frameworks library to generate an OpenAPI document called openapi.json
. This file describes the sample code's REST API.
To generate the OpenAPI configuration file:
Invoke the Endpoints Frameworks tool using this command:
mvn endpoints-framework:openApiDocs
Wait for the configuration spec to build. When it finishes a message similar to this one displays:
OpenAPI document written to target/openapi-docs/openapi.json
Ignore any messages about failure to load a static logger class.
To deploy the Endpoints configuration, you use Service Infrastructure, Google’s foundational services platform, used by Endpoints Frameworks and other services to create and manage APIs and services
To deploy the configuration file:
Make sure you are in the directory java-docs-samples/appengine-java8/endpoints-v2-backend
.
Deploy the OpenAPI configuration file that was generated in the previous section:
gcloud endpoints services deploy target/openapi-docs/openapi.json
This creates a new Endpoints service with the name in the format YOUR_PROJECT_ID.appspot.com
. This name is configured in pom.xml
and other configuration files included in the sample. Note that when you deploy the API on App Engine, a DNS record is created using the name format YOUR_PROJECT_ID.appspot.com
, which is the fully qualified domain name (FQDN) that you use when you send requests to the API.
As it is creating and configuring the service, Service Management outputs information to the terminal. You can safely ignore the warnings about the paths in openapi.json
not requiring an API key. On successful completion, a line similar to the following displays the service configuration ID and the service name:
Service Configuration [2017-02-13-r2] uploaded for service [example-project-12345.appspot.com]
In the preceding example, 2017-02-13-r2
is the service configuration ID and example-project-12345.appspot.com
is the service name.
See gcloud
Endpoints services deploy for more information.
servicemanagement.googleapis.com
Service Management API servicecontrol.googleapis.com
Service Control API
In most cases, the gcloud endpoints services deploy
command enables these required services. However, the gcloud
command completes successfully but doesn't enable the required services in the following circumstances:
If you used a third-party application such as Terraform, and you don't include these services.
You deployed the Endpoints configuration to an existing Google Cloud project in which these services were explicitly disabled.
Use the following command to confirm that the required services are enabled:
gcloud services list
If you do not see the required services listed, enable them:
gcloud services enable servicemanagement.googleapis.com
gcloud services enable servicecontrol.googleapis.com
Also enable your Endpoints service:
gcloud services enable ENDPOINTS_SERVICE_NAME
To determine the ENDPOINTS_SERVICE_NAME you can either:
After deploying the Endpoints configuration, go to the Endpoints page in the Cloud console. The list of possible ENDPOINTS_SERVICE_NAME are shown under the Service name column.
For OpenAPI, the ENDPOINTS_SERVICE_NAME is what you specified in the host
field of your OpenAPI spec. For gRPC, the ENDPOINTS_SERVICE_NAME is what you specified in the name
field of your gRPC Endpoints configuration.
For more information about the gcloud
commands, see gcloud
services.
After deploying the Endpoints configuration, you can run the sample locally.
Create an environment variable called ENDPOINTS_SERVICE_NAME
, which is used in the sample's appengine-web.xml
file to set the hostname. In the following, replace YOUR_PROJECT_ID
with your Google Cloud project ID.
In Linux or Mac OS:
export ENDPOINTS_SERVICE_NAME=YOUR_PROJECT_ID.appspot.com
In Windows PowerShell:
$Env:ENDPOINTS_SERVICE_NAME="YOUR_PROJECT_ID.appspot.com"
Acquire new user credentials to use for Application Default Credentials:
gcloud auth application-default login
Run the development server:
mvn appengine:run
The local instance is reachable on http://localhost:8080
as indicated by the logs printed by the mvn appengine:run
command:
[INFO] GCLOUD: INFO: Module instance default is running at http://localhost:8080/
Send a request to the local instance:
curl \
--request POST \
--header "Content-Type: application/json" \
--data '{"message":"hello world"}' \
http://localhost:8080/_ah/api/echo/v1/echo
In the preceding curl
:
--data
option specifies the data to post to the API.--header
option specifies that the data is in JSON format.(Invoke-WebRequest -Method POST -Body '{"message": "hello world"}' `
-Headers @{"content-type"="application/json"} `
-URI "http://localhost:8080/_ah/api/echo/v1/echo").Content
In the previous example, the first two lines end in a backtick. When you paste the example into PowerShell, make sure there isn't a space following the backticks. For information about the options used in the example request, see Invoke-WebRequest in the Microsoft documentation.
The API echoes back the message that you send it, and responds with the following:
{
"message": "hello world"
}
Deploying the API backend
So far you have deployed the OpenAPI configuration to Service Management, but you haven't yet deployed the code that serves the API backend. This section walks you through deploying the sample API to App Engine.
To deploy the API backend:
Make sure you are in the directory java-docs-samples/appengine-java8/endpoints-v2-backend
Deploy the API implementation code using Maven:
mvn appengine:deploy
The first time you upload a sample app, you might be prompted to authorize the deployment. Follow 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.
We recommend that you wait a few minutes before sending requests to your API while App Engine completely initializes.
After you deploy the API and its configuration file, you can send requests to the API.
Linux or Mac OSSend an HTTP request by using curl
. Replace [YOUR_PROJECT_ID]
with your Google Cloud project ID:
curl \
--request POST \
--header "Content-Type: application/json" \
--data '{"message":"hello world"}' \
https://[YOUR_PROJECT_ID].appspot.com/_ah/api/echo/v1/echo
In the preceding curl
:
--data
option specifies the data to post to the API.--header
option specifies that the data is in JSON format.Send an HTTP request by using Invoke-WebRequest
. Replace [YOUR_PROJECT_ID]
with your Google Cloud project ID:
(Invoke-WebRequest -Method POST -Body '{"message": "hello world"}' `
-Headers @{"content-type"="application/json"} -URI `
"https://[YOUR_PROJECT_ID].appspot.com/_ah/api/echo/v1/echo").Content
In the previous example, the first two lines end in a backtick. When you paste the example into PowerShell, make sure there isn't a space following the backticks. For information about the options used in the example request, see Invoke-WebRequest in the Microsoft documentation.
Third-party appYou can use a third-party application such as the Chrome browser extension Postman to send the request:
POST
as the HTTP verb.content-type
and the value application/json
. {"message":"hello world"}
https://example-project-12345.appspot.com/_ah/api/echo/v1/echo
The API echoes back the message that you send it, and responds with the following:
{
"message": "hello world"
}
If you didn't get a successful response, see Troubleshooting response errors.
You just deployed and tested an API in Endpoints Frameworks!
Note: If you want to let us know what you liked about this tutorial, feel free to provide feedback at the bottom of this page. Your feedback helps us improve these tutorials.google-cloud-endpoints
Google group and let us know what went wrong. This group receives responses from other Endpoints users and from Google engineers, who monitor the group. Tracking API activity
View the activity graphs for your API in the Google Cloud console on the Endpoints > Service page.
Go to the Endpoints Services page
It might take a few moments for the request to be reflected in the graphs.
Look at the request logs for your API in the Logs Explorer page.
You can use Cloud Endpoints Portal to create a developer portal, a website that you can use to interact with the sample API. To learn more, see Cloud Endpoints Portal overview.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4