Stay organized with collections
Save and categorize content based on your preferences.
This page describes how to structure and annotate your
Cloud Endpoints Frameworks code. For a complete list of all supported
annotations, see
Annotations.
The skeleton Endpoints Frameworks example is located in:
cdappengine-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.java that contains the following
code:
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 echo method as your first API endpoint and the
doEcho helper method to your MyApi.java.
Copy all of the imports from
Echo.java, and paste them in your MyApi.java.
Maven
Build the project:
mvncleanpackage
Gradle
Build the project:
gradlecleanbuild
Annotation basics
There are three annotations commonly used in backend APIs:
@Api
contains the configuration details of the backend API.
@ApiMethod
marks a class method that is part of the backend API. Methods that
aren't marked with@ApiMethod aren't included when you generate client
libraries and discovery docs. The @ApiMethod annotation can also be used
to override the API configuration for a specific method.
@Named must 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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-21 UTC."],[[["\u003cp\u003eThis page guides you through structuring and annotating Cloud Endpoints Frameworks code, utilizing the \u003ccode\u003eendpoints-v2-skeleton\u003c/code\u003e sample project.\u003c/p\u003e\n"],["\u003cp\u003eThe document explains how to use annotations like \u003ccode\u003e@Api\u003c/code\u003e, \u003ccode\u003e@ApiMethod\u003c/code\u003e, and \u003ccode\u003e@Named\u003c/code\u003e to define and configure your backend API.\u003c/p\u003e\n"],["\u003cp\u003eYou'll learn how to modify the \u003ccode\u003eMyApi.java\u003c/code\u003e file by defining API versions and adding an \u003ccode\u003eecho\u003c/code\u003e method as your first API endpoint.\u003c/p\u003e\n"],["\u003cp\u003eThe document uses the \u003ccode\u003eendpoints-v2-backend\u003c/code\u003e sample as a reference, so the end result of modifying the \u003ccode\u003eendpoints-v2-skeleton\u003c/code\u003e will behave identically.\u003c/p\u003e\n"],["\u003cp\u003eBuilding the project will require you to use Maven or Gradle, depending on which system you use.\u003c/p\u003e\n"]]],[],null,["# Writing and annotating the code\n\nThis page describes how to structure and annotate your\nCloud Endpoints Frameworks code. For a complete list of all supported\nannotations, see\n[Annotations](/endpoints/docs/frameworks/java/annotations).\n\nBefore you begin\n----------------\n\n1. [Set up your development environment](/endpoints/docs/frameworks/java/set-up-environment).\n2. Clone the skeleton Endpoints Frameworks example:\n\n git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git\n\n3. The skeleton Endpoints Frameworks example is located in:\n\n cd appengine-java8/endpoints-v2-skeleton/\n\nTo help explain how annotations work, this document uses the\n`endpoints-v2-backend` sample to show the annotations and other code that you\nmust add to the `endpoints-v2-skeleton` sample to get it to build. In the end,\nthe modified `endpoints-v2-skeleton` sample behaves the same as the\n`endpoints-v2-backend` sample, which is used in\n[Getting started with Endpoints Frameworks on App Engine](/endpoints/docs/frameworks/java/get-started-frameworks-java).\n\nCreating and annotating code\n----------------------------\n\nTo annotate your code:\n\n1. Change directories to the project's Java source directory, for example: `src/main/java/com/example/skeleton`.\n2. Create a [JavaBean](https://wikipedia.org/wiki/JavaBeans) class file named `Message.java` that contains the following code: \n\n public class Message {\n\n private String message;\n\n public String getMessage() {\n return this.message;\n }\n\n public void setMessage(String message) {\n this.message = message;\n }\n }\n\n3. Edit the `MyApi.java` file contained in the skeleton example. Change the `@Api` definition annotation with the following: \n\n @Api(\n name = \"echo\",\n version = \"v1\",\n namespace =\n @ApiNamespace(\n ownerDomain = \"echo.example.com\",\n ownerName = \"echo.example.com\",\n packagePath = \"\"\n ),\n // ...\n )\n\n The `version = \"v1\"` attribute specifies the version of the sample\n API. The value that you enter becomes part of the path in the URL to your\n API. For more information on versions, see\n [Handling API versioning](/endpoints/docs/frameworks/java/handling-api-versioning).\n4. Add the following `echo` method as your first API endpoint and the `doEcho` helper method to your `MyApi.java`. \n\n @ApiMethod(name = \"echo\")\n public Message echo(Message message, @Named(\"n\") @Nullable Integer n) {\n return doEcho(message, n);\n }\n\n private Message doEcho(Message request, Integer n) {\n Message response = new Message();\n if (n != null && n \u003e= 0) {\n StringBuilder sb = new StringBuilder();\n for (int i = 0; i \u003c n; i++) {\n if (i \u003e 0) {\n sb.append(' ');\n }\n sb.append(request.getMessage());\n }\n response.setMessage(sb.toString());\n }\n return response;\n }\n\n5. Copy all of the imports from [`Echo.java`](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/appengine-java8/endpoints-v2-backend/src/main/java/com/example/echo/Echo.java), and paste them in your `MyApi.java`.\n6.\n\n ### Maven\n\n Build the project: \n\n ```bash\n mvn clean package\n ```\n\n ### Gradle\n\n Build the project: \n\n ```bash\n gradle clean build\n ```\n\nAnnotation basics\n-----------------\n\nThere are three annotations commonly used in backend APIs:\n\n- [`@Api`](/endpoints/docs/frameworks/java/annotations#api_api-scoped_annotations) contains the configuration details of the backend API.\n- [`@ApiMethod`](/endpoints/docs/frameworks/java/annotations#apimethod_method-scoped_annotations) marks a class method that is part of the backend API. Methods that aren't marked with`@ApiMethod` aren't included when you generate client libraries and discovery docs. The `@ApiMethod` annotation can also be used to override the API configuration for a specific method.\n- [`@Named`](/endpoints/docs/frameworks/java/annotations#named) must be added to all parameters passed to server-side methods, unless the parameter is an entity type.\n\nFor a complete list of all Endpoints Frameworks annotations, see\n[Annotations and syntax](/endpoints/docs/frameworks/java/annotations).\n\nWhat's next\n-----------\n\n- [Learn about adding API management](/endpoints/docs/frameworks/java/adding-api-management).\n- [Learn about the supported parameter and return types](/endpoints/docs/frameworks/java/parameter-and-return-types).\n- [Learn about exceptions and status codes](/endpoints/docs/frameworks/java/exceptions)."]]