Write your web service Stay organized with collections Save and categorize content based on your preferences.
This guide shows how to write a Java web service to run in the App Enginestandard environment. To learn more about the Java runtime and how itworks, seeJava Runtime Environment.
Before you begin
If you haven't already:
- Install the latest version of the Java Development Kit (JDK) for the App Engine runtime versionyou plan to use.
- Download andinstall Apache Maven to build, run, and deploy the sample app.
Key points
- App Engine starts your application by uploading an executable JARapplication.
- Your application must have a main class that starts a web server whichresponds to HTTP requests on the port specified by the PORT environmentvariable, typically 8080.
- You need an
app.yamlfile to deploy your service to App Engine. - You can use dependencies by listing them in your
pom.xmlfile. For more information, seeUsing Java libraries.
Create a main class
The core of your web service is the HTTP server. The sample code in this guideuses theSpring Boot framework to handle HTTP requests, but you are free to use a web framework ofyour choice.
Generate a Spring Boot project for Java that uses the Maven build system and contains theSpring Web dependency. To get started, click the following link:
In Spring Initializer, click theGenerate button to generate and download your project.
In the downloaded project, edit the file
springboot/src/main/java/com/example/appengine/springboot/DemoApplication.javato add some java imports and a REST hello handler:packagecom.example.appengine.springboot;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestControllerpublicclassDemoApplication{publicstaticvoidmain(String[]args){SpringApplication.run(DemoApplication.class,args);}@GetMapping("/")publicStringhello(){return"Hello world!";}}The modified class is a controller that starts Spring Boot's embedded Tomcatserver and responds to
GETrequests at the root path ('/') with the text "Hello world!"
Run the server locally
To run the server locally:
Start a local web server using the Spring Boot Maven plugin.
mvnspring-boot:runIn your web browser, enter the following address:
http://localhost:8080
TheHello World message from the sample app displays on the page. In yourterminal window, pressCtrl+C to exit the web server.
Create theapp.yaml file
To specify settings for your app in the App Engine runtime environment:
Create a file named
app.yamlin the following directory:springboot/src/main/appengine/Add the following contents to the file:
Java 21
runtime: java21The
app.yamlfile can also specify network settings, scaling settings,and more. For more information, see theapp.yamlreference.
If you used theSpring Initializr link above, you should now have a file structure like the following:
springboot/pom.xmlsrc/main/appengine/app.yaml
java/com/example/appengine/springboot/DemoApplication.java
You can also add in the project pom.xml the Maven plugin that allows deployment of the application:
<plugin><groupId>com.google.cloud.tools</groupId><artifactId>appengine-maven-plugin</artifactId><version>2.8.1</version><configuration><projectId>YOUR_PROJECT_NAME</projectId><version>YOUR_VERSION</version><promote>false</promote></configuration></plugin>Next steps
Now that you've created a simple Java web server that listens to the correctport and you've specified the runtime in anapp.yaml file, you're ready todeploy your service on App Engine.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.