Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questa pagina descrive come strutturare e annotare il codice di
Cloud Endpoints Frameworks. Per un elenco completo di tutte le annotazioni supportate, consulta la sezione
Annotazioni.
L'esempio di base di Endpoints Frameworks si trova in:
cdappengine-java8/endpoints-v2-skeleton/
Per spiegare meglio come funzionano le annotazioni, questo documento utilizza l'esempio
endpoints-v2-backend per mostrare le annotazioni e altro codice che devi
aggiungere all'esempio endpoints-v2-skeleton per compilarlo. Alla fine,
l'esempio endpoints-v2-skeleton modificato si comporta allo stesso modo dell'esempio endpoints-v2-backend, che viene utilizzato in
Guida introduttiva a Endpoints Frameworks su App Engine.
Creazione e annotazione del codice
Per annotare il codice:
Passa alla directory di origine Java del progetto, ad esempio:
src/main/java/com/example/skeleton.
Crea un file di classe JavaBean
denominato Message.java che contenga il seguente
codice:
L'attributo version = "v1" specifica la versione dell'API di esempio. Il valore che inserisci diventa parte del percorso nell'URL della tua API. Per ulteriori informazioni sulle versioni, consulta
Gestione del controllo delle versioni dell'API.
Aggiungi il seguente metodo echo come primo endpoint API e il
metodo helper doEcho al tuo MyApi.java.
Copia tutte le importazioni da
Echo.java e incollale in MyApi.java.
Maven
Crea il progetto:
mvncleanpackage
Gradle
Crea il progetto:
gradlecleanbuild
Nozioni di base sulle annotazioni
Esistono tre annotazioni comunemente utilizzate nelle API di backend:
@Api
contiene i dettagli di configurazione dell'API di backend.
@ApiMethod
contrassegna un metodo di classe che fa parte dell'API backend. I metodi che
non sono contrassegnati con@ApiMethod non sono inclusi quando generi librerie client
e documenti di rilevamento. L'annotazione @ApiMethod può essere utilizzata anche
per sostituire la configurazione dell'API per un metodo specifico.
@Named deve essere aggiunto
a tutti i parametri passati ai metodi lato server, a meno che
il parametro non sia un tipo di entità.
Per un elenco completo di tutte le annotazioni di Endpoints Frameworks, consulta
Annotazioni e sintassi.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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)."]]