Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Auf dieser Seite wird beschrieben, wie Sie Ihren Cloud Endpoints Frameworks-Code strukturieren und annotieren. Eine vollständige Liste aller unterstützten Annotationen finden Sie unter Annotationen.
Das Endpoints Frameworks-Basisbeispiel befindet sich hier:
cdappengine-java8/endpoints-v2-skeleton/
Zur Erläuterung der Funktionsweise von Annotationen wird in diesem Dokument das Beispiel endpoints-v2-backend verwendet. Es zeigt die Annotationen und anderen Code, den Sie in das Beispiel endpoints-v2-skeleton einfügen müssen. Am Ende verhält sich das geänderte Beispiel endpoints-v2-skeleton genauso wie das Beispiel endpoints-v2-backend, das in Erste Schritte mit Endpoints Frameworks in App Engine verwendet wird.
Code erstellen und annotieren
So annotieren Sie den Code:
Rufen Sie das Java-Quellverzeichnis des Projekts auf. Beispiel: src/main/java/com/example/skeleton.
Erstellen Sie eine JavaBean-Klassendatei mit dem Namen Message.java, die den folgenden Code enthält:
Das Attribut version = "v1" gibt die Version der Beispiel-API an. Der Wert, den Sie eingeben, wird Teil des Pfads in der URL zu Ihrer API. Weitere Informationen zu Versionen finden Sie unter API-Versionierung handhaben.
Fügen Sie die folgende echo-Methode als ersten API-Endpunkt und die doEcho-Hilfsmethode zu MyApi.java hinzu.
Kopieren Sie alle Importe aus Echo.java und fügen Sie sie in MyApi.java ein.
Maven
Erstellen Sie das Projekt:
mvncleanpackage
Gradle
Erstellen Sie das Projekt:
gradlecleanbuild
Grundlagen zur Annotation
In Backend-APIs werden generell drei Annotationen verwendet.
@Api enthält die Konfigurationsdetails der Backend-API.
@ApiMethod markiert eine Klassenmethode, die Teil der Backend-API ist. Methoden, die nicht mit @ApiMethod markiert sind, werden beim Generieren von Clientbibliotheken und Discovery-Dokumenten nicht einbezogen. Sie können mit der @ApiMethod-Annotation auch die API-Konfiguration für eine bestimmte Methode überschreiben.
@Named muss allen Parametern hinzugefügt werden, die an serverseitige Methoden übergeben werden, sofern der Parameter kein Entitätstyp ist.
Eine vollständige Liste aller Endpoints Frameworks-Annotationen finden Sie unter Annotationen und Syntax.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (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)."]]