Write your web service

Note: If you are deploying a new Java web service to Google Cloud,we recommend getting started withCloud Run.

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:

  1. Install the latest version of the Java Development Kit (JDK) for the App Engine runtime versionyou plan to use.
  2. 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 anapp.yamlfile to deploy your service to App Engine.
  • You can use dependencies by listing them in yourpom.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.

  1. 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:

    Go to Spring Initializr

  2. In Spring Initializer, click theGenerate button to generate and download your project.

  3. In the downloaded project, edit the filespringboot/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 toGET requests at the root path ('/') with the text "Hello world!"

Run the server locally

To run the server locally:

  1. Start a local web server using the Spring Boot Maven plugin.

    mvnspring-boot:run
  2. In 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:

  1. Create a file namedapp.yaml in the following directory:
    springboot/src/main/appengine/

  2. Add the following contents to the file:

    Java 21

    runtime: java21

    Theapp.yaml file can also specify network settings, scaling settings,and more. For more information, see theapp.yaml reference.

If you used theSpring Initializr link above, you should now have a file structure like the following:

  • springboot/
    • pom.xml
    • src/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.