Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Dieser Leitfaden zeigt, wie ein Java-Webdienst geschrieben wird, der in der App Engine-Standardumgebung ausgeführt wird. Weitere Informationen zur Java-Laufzeit und ihrer Funktionsweise finden Sie unter Java-Laufzeitumgebung.
Hinweise
Vorbereitende Schritte (sofern noch nicht ausgeführt):
Apache Maven herunterladen und installieren, um die Beispielanwendung zu erstellen, auszuführen und bereitzustellen
Wichtige Fakten
App Engine startet Ihre Anwendung durch das Hochladen einer ausführbaren JAR-Anwendung.
Ihre Anwendung muss eine Hauptklasse haben, die einen Webserver startet. Dieser antwortet auf HTTP-Anfragen von dem Port, der mit der PORT-Umgebungsvariablen festgelegt wurde, in der Regel 8080.
Sie benötigen eine Datei app.yaml, um Ihren Dienst in App Engine bereitzustellen.
Sie können Abhängigkeiten verwenden, indem Sie diese in die Datei pom.xml aufnehmen. Weitere Informationen finden Sie unter Java-Bibliotheken verwenden.
Hauptklasse erstellen
Der HTTP-Server bildet den Kern Ihres Webdiensts. Der Beispielcode in diesem Leitfaden verwendet zur Verarbeitung von HTTP-Anfragen das Spring Boot-Framework. Sie können aber auch ein Web-Framework Ihrer Wahl einsetzen.
Erstellen Sie ein Spring Boot-Projekt für Java, das das Maven-Build-System verwendet und die Spring Web-Abhängigkeit enthält. Klicken Sie zuerst auf den folgenden Link:
Klicken Sie in Spring Initializer auf die Schaltfläche Generate, um Ihr Projekt zu erstellen und herunterzuladen.
Bearbeiten Sie im heruntergeladenen Projekt die Datei springboot/src/main/java/com/example/appengine/springboot/DemoApplication.java, um einige Java-Importe und einen REST-Hello-Handler hinzuzufügen:
Die geänderte Klasse ist ein Controller, der den eingebetteten Tomcat-Server von Spring Boot startet und auf GET-Anfragen im Stammpfad ('/') mit dem Text „Hello world!“ antwortet.
Server lokal ausführen
So führen Sie den Server lokal aus:
Starten Sie einen lokalen Webserver mithilfe des Spring Boot-Maven-Plug-ins.
Auf der Seite wird die Nachricht Hello World aus der Beispielanwendung angezeigt. Drücken Sie im Terminalfenster Strg + C, um den Webserver zu beenden.
Erstellen der app.yaml-Datei
So legen Sie Einstellungen für Ihre Anwendung in der App Engine-Laufzeitumgebung fest:
Erstellen Sie eine Datei mit dem Namen app.yaml im folgenden Verzeichnis: springboot/src/main/appengine/
Fügen Sie der Datei den folgenden Inhalt hinzu:
Java 21
runtime: java21
In der Datei app.yaml können unter anderem auch Netzwerk- und Skalierungseinstellungen festgelegt werden. Weitere Informationen finden Sie in der Referenz zu app.yaml.
Wenn Sie den obigen Spring Initializr-Link verwendet haben, sollten Sie nun eine Dateistruktur wie die folgende haben:
springboot/
pom.xml
src/main/
appengine/
app.yaml
java/com/example/appengine/springboot/
DemoApplication.java
Sie können auch das Maven-Plug-in, das die Bereitstellung der Anwendung ermöglicht, in der pom.xml des Projekts hinzufügen:
Sie haben jetzt einen einfachen Java-Webserver erstellt, der den richtigen Port überwacht, und Sie haben die Laufzeit in der Datei app.yaml angegeben. Als Nächstes können Sie den Dienst in App Engine bereitstellen.
[[["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 guide outlines how to create a Java web service for the App Engine standard environment, starting with a recommendation to consider Cloud Run for new Java web service deployments on Google Cloud.\u003c/p\u003e\n"],["\u003cp\u003eApp Engine requires an executable JAR application with a main class that launches a web server, listening for HTTP requests on the port defined by the PORT environment variable, typically 8080.\u003c/p\u003e\n"],["\u003cp\u003eThe deployment of a web service to App Engine necessitates an \u003ccode\u003eapp.yaml\u003c/code\u003e file for configuration, in addition to listing any dependencies in a \u003ccode\u003epom.xml\u003c/code\u003e file.\u003c/p\u003e\n"],["\u003cp\u003eA sample Spring Boot project is provided as an example, which includes generating the project through Spring Initializr and modifying a specific file, \u003ccode\u003eDemoApplication.java\u003c/code\u003e, to include the code for a Rest controller.\u003c/p\u003e\n"],["\u003cp\u003eAfter creating a web server that listens on the correct port and specifying the runtime environment in \u003ccode\u003eapp.yaml\u003c/code\u003e, the next step is deploying your service to App Engine.\u003c/p\u003e\n"]]],[],null,["# Write your web service\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\n| **Note:** If you are deploying a new Java web service to Google Cloud, we recommend getting started with [Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-java-service).\n\nThis guide shows how to write a Java web service to run in the App Engine\nstandard environment. To learn more about the Java runtime and how it\nworks, see [Java Runtime Environment](/appengine/docs/standard/java-gen2/runtime).\n\nBefore you begin\n----------------\n\nIf you haven't already:\n\n1. [Install the latest version of the Java Development Kit (JDK)](http://www.oracle.com/technetwork/java/javase/downloads/index.html) for the App Engine runtime version you plan to use.\n2. [Download](https://maven.apache.org/download.cgi) and [install](https://maven.apache.org/install.html) Apache Maven to build, run, and deploy the sample app.\n\nKey points\n----------\n\n- App Engine starts your application by uploading an executable JAR application.\n- Your application must have a main class that starts a web server which responds to HTTP requests on the port specified by the PORT environment variable, typically 8080.\n- You need an [`app.yaml`](/appengine/docs/standard/java-gen2/configuring-your-app-with-app-yaml) file to deploy your service to App Engine.\n- You can use dependencies by listing them in your [`pom.xml`](https://maven.apache.org/pom.html) file. For more information, see [Using Java libraries](/appengine/docs/standard/java-gen2/specifying-dependencies).\n\nCreate a main class\n-------------------\n\nThe core of your web service is the HTTP server. The sample code in this guide\nuses the [Spring Boot](https://spring.io/)\nframework to handle HTTP requests, but you are free to use a web framework of\nyour choice.\n\n1. Generate a Spring Boot project for Java that uses the Maven build system and contains the\n Spring Web dependency. To get started, click the following link:\n\n [Go to Spring Initializr](https://start.spring.io/#!type=maven-project&language=java&packaging=jar&jvmVersion=21&groupId=com.example.appengine&artifactId=springboot&packageName=com.example.appengine.springboot&dependencies=web)\n2. In Spring Initializer, click the **Generate** button to generate and download your project.\n\n3. In the downloaded project, edit the file `springboot/src/main/java/com/example/appengine/springboot/DemoApplication.java`\n to add some java imports and a REST hello handler:\n\n package com.example.appengine.springboot;\n import org.springframework.boot.SpringApplication;\n import org.springframework.boot.autoconfigure.SpringBootApplication;\n import org.springframework.web.bind.annotation.GetMapping;\n import org.springframework.web.bind.annotation.RestController;\n @SpringBootApplication\n @RestController\n public class DemoApplication {\n public static void main(String[] args) {\n SpringApplication.run(DemoApplication.class, args);\n }\n @GetMapping(\"/\")\n public String hello() {\n return \"Hello world!\";\n }\n }\n\n The modified class is a controller that starts Spring Boot's embedded Tomcat\n server and responds to `GET` requests at the root path (`'/'`) with the text \"Hello world!\"\n\nRun the server locally\n----------------------\n\nTo run the server locally:\n\n1. Start a local web server using the Spring Boot Maven plugin.\n\n mvn spring-boot:run\n\n2. In your web browser, enter the following address: \n\n \u003chttp://localhost:8080\u003e\n\nThe **Hello World** message from the sample app displays on the page. In your\nterminal window, press **Ctrl+C** to exit the web server.\n\nCreate the `app.yaml` file\n--------------------------\n\nTo specify settings for your app in the App Engine runtime environment:\n\n1. Create a file named `app.yaml` in the following directory: \n\n `springboot/src/main/appengine/`\n\n2. Add the following contents to the file:\n\n ### Java 21\n\n runtime: java21\n\n The `app.yaml` file can also specify network settings, scaling settings,\n and more. For more information, see the\n [`app.yaml` reference](/appengine/docs/standard/reference/app-yaml).\n\nIf you used the *Spring Initializr* link above, you should now have a file structure like the following:\n\n- `springboot/`\n - `pom.xml`\n - `src/main/`\n - `appengine/`\n - `app.yaml`\n - `java/com/example/appengine/springboot/`\n - `DemoApplication.java`\n\nYou can also add in the project pom.xml the Maven plugin that allows deployment of the application: \n\n \u003cplugin\u003e\n \u003cgroupId\u003ecom.google.cloud.tools\u003c/groupId\u003e\n \u003cartifactId\u003eappengine-maven-plugin\u003c/artifactId\u003e\n \u003cversion\u003e2.8.1\u003c/version\u003e\n \u003cconfiguration\u003e\n \u003cprojectId\u003eYOUR_PROJECT_NAME\u003c/projectId\u003e\n \u003cversion\u003eYOUR_VERSION\u003c/version\u003e\n \u003cpromote\u003efalse\u003c/promote\u003e\n \u003c/configuration\u003e\n \u003c/plugin\u003e\n\nNext steps\n----------\n\nNow that you've created a simple Java web server that listens to the correct\nport and you've specified the runtime in an `app.yaml` file, you're ready to\n[deploy your service on App Engine](/appengine/docs/standard/java-gen2/building-app/deploying-web-service)."]]