Writing Your Java 11 Web Service

This guide shows how to write a Java 11 web service to run in the App Engine standard environment. To learn more about the Java 11 runtime and how it works, see Java 11 Runtime Environment.

Before you begin

If you haven't already:

  1. Install the Java SE 11 Development Kit (JDK)
  2. Download and install Apache Maven to build, run, and deploy the sample app.

Key points

  • App Engine starts your application by uploading an executable JAR application.
  • 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.
  • You need an app.yaml file to deploy your service to App Engine.
  • You can use dependencies by listing them in your pom.xml file. For more information, see Using Java libraries.

Creating a main class

The core of your web service is the HTTP server. The sample code in this guide uses the Spring Boot framework to handle HTTP requests, but you are free to use a web framework of your choice.

  1. Generate a Spring Boot project choosing Java and the Maven build system:

    Go to Spring Initializr

  2. Add the following imports and code to create a controller which responds to all GET requests to the root path ('/') with the text "Hello world!":

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RestController
    public class SpringbootApplication {
    
      public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
      }
    
      @GetMapping("/")
      public String hello() {
        return "Hello world!";
      }
    
    }

    The SpringbootApplication controller starts Spring Boot's embedded Tomcat server which listens to the default port 8080.

Running the server locally

To run the server locally:

  1. Use the App Engine plugin to build, package, and stage the App Engine staging directory.

    mvn spring-boot:run
    
  2. Point your web browser to http://localhost:8080.

You should see a page with the text "Hello world!"

Adding the App Engine Maven plugin

To deploy using the App Engine Maven plugin, add the following into the plugins section in the project pom.xml file:

<plugin>
   <groupId>com.google.cloud.tools</groupId>
   <artifactId>appengine-maven-plugin</artifactId>
   <version>2.2.0</version>
</plugin>

Creating the app.yaml file

An app.yaml file specifies settings for your App Engine service's runtime environment. Your service will not deploy without this file.

  1. Create your my-java-service/src/main/appengine/app.yaml file.

  2. Add the following contents:

    runtime: java11
    instance_class: F1
    
    # Explicitly set the memory limit and maximum heap size for the Spring Boot app
    env_variables:
      JAVA_TOOL_OPTIONS: "-XX:MaxRAM=256m -XX:ActiveProcessorCount=2 -Xmx32m"

    This is a minimal configuration file, indicating to App Engine the version of the Java 11 runtime. The app.yaml file can also specify network settings, scaling settings, and more. For more information, see the app.yaml reference.

At this point, you should have a file structure like the following:

  • my-java-service/
    • pom.xml
    • src/main/
      • appengine/
        • app.yaml
      • java/com/example/demo/
        • DemoApplication.java

Next steps

Now that you've created a simple Java 11 web server that listens to the correct port and you've specified the runtime in an app.yaml file, you're ready to deploy your service on App Engine.