This page describes how to structure and annotate your Cloud Endpoints Frameworks code. For a complete list of all supported annotations, see Annotations.
Before you begin
- Set up your development environment.
Clone the skeleton Endpoints Frameworks example:
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.gitThe skeleton Endpoints Frameworks example is located in:
cd appengine-java8/endpoints-v2-skeleton/
To help explain how annotations work, this document uses the
endpoints-v2-backend sample to show the annotations and other code that you
must add to the endpoints-v2-skeleton sample to get it to build. In the end,
the modified endpoints-v2-skeleton sample behaves the same as the
endpoints-v2-backend sample, which is used in
Getting started with Endpoints Frameworks on App Engine.
Creating and annotating code
To annotate your code:
-
Change directories to the project's Java source directory, for example:
src/main/java/com/example/skeleton. -
Create a JavaBean
class file named
Message.javathat contains the following code: -
Edit the
MyApi.javafile contained in the skeleton example. Change the@Apidefinition annotation with the following:The
version = "v1"attribute specifies the version of the sample API. The value that you enter becomes part of the path in the URL to your API. For more information on versions, see Handling API versioning. -
Add the following
echomethod as your first API endpoint and thedoEchohelper method to yourMyApi.java. -
Copy all of the imports from
Echo.java, and paste them in yourMyApi.java. -
Maven
Build the project:
mvn clean package
Gradle
Build the project:
gradle clean build
Annotation basics
There are three annotations commonly used in backend APIs:
@Apicontains the configuration details of the backend API.@ApiMethodmarks a class method that is part of the backend API. Methods that aren't marked with@ApiMethodaren't included when you generate client libraries and discovery docs. The@ApiMethodannotation can also be used to override the API configuration for a specific method.@Namedmust be added to all parameters passed to server-side methods, unless the parameter is an entity type.
For a complete list of all Endpoints Frameworks annotations, see Annotations and syntax.
What's next
- Learn about adding API management.
- Learn about the supported parameter and return types.
- Learn about exceptions and status codes.