Google App Engine

Configuring the Maven Project for Endpoints

Before adding code and testing it, you need to make sure the project has all the necessary dependencies and project settings. Make sure you complete all of the configurations described below.

Adding the Project ID to appengine-web.xml

To add the project ID:

  1. Navigate to the project in helloendpoints/helloendpoints-war.
  2. Open the file src/main/webapp/WEB-INF/appengine-web.xml in an editor.
  3. In the <application> field, copy the project ID (App Engine app ID). This is the value you obtained following the Setup instructions to create the project. (The backend API will be deployed to this project ID.)
  4. In the <version> field, set the value to 1. (For more information about deploying to application versions, see Deploying to multiple App Engine app versions.)
  5. Save your work and close the file.

Configuring web.xml

To configure web.xml:

  1. Locate the file helloendpoints/helloendpoints-war/src/main/webapp/WEB-INF/web.xml and open it in an editor.
  2. Replace the file contents with the following:

    <?xml version="1.0" encoding="utf-8" standalone="no"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <servlet>
            <servlet-name>SystemServiceServlet</servlet-name>
            <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
            <init-param>
                <param-name>services</param-name>
                <param-value>com.google.appengine.samples.helloendpoints.Greetings</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>SystemServiceServlet</servlet-name>
            <url-pattern>/_ah/spi/*</url-pattern>
        </servlet-mapping>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
    </web-app>
    

Setting required properties entries

To set the properties:

  1. Navigate to the helloendpoints/helloendpoints-war directory, and open the pom.xml file with an editor.
  2. Locate the <properties> entries in the default pom.xml file:
  3. In <properties>, locate the line <appengine.app.version>version</appengine.app.version>, and add the line

    <appengine.target.version><!--version--></appengine.target.version>
    

    replacing <!--Latest App Engine Java SDK version--> with the latest App Engine Java SDK version: 1.9.1. When finished, the entries should look something like this:

    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    
    <groupId>com.google.appengine.samples.helloendpoints</groupId>
    <artifactId>helloendpoints</artifactId>
    
    <properties>
        <appengine.app.version>1</appengine.app.version>
        <appengine.target.version><!--Latest App Engine Java SDK version--></appengine.target.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
  4. Make sure that <packaging> is set to war.

  5. Keep the pom.xml file open and set dependencies configuration as described in the next section.

Setting required runtime dependencies

To set dependencies:

  1. In pom.xml, locate the dependencies under the comment Compile/runtime dependencies.
  2. Replace all of those entries with the following:

    <!-- Compile/runtime dependencies -->
    <dependency>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-api-1.0-sdk</artifactId>
        <version>${appengine.target.version}</version>
    </dependency>
    <dependency>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-endpoints</artifactId>
        <version>${appengine.target.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    

    Notice in the above lines that javax.inject is not absolutely required, but it is commonly used, since any parameter injection using @Named in Endpoints must use javax.inject for this. It is required for this tutorial.

  3. Keep the pom.xml file open and set plugin configuration as described in the next sections.

Setting required plugin configuration

To set the default plugin configuration:

  1. In pom.xml, locate and replace the default maven-war-plugin configuration entirely with the following:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
            <webXml>${project.build.directory}/generated-sources/appengine-endpoints/WEB-INF/web.xml</webXml>
            <webResources>
                <resource>
                    <!-- this is relative to the pom.xml directory -->
                    <directory>${project.build.directory}/generated-sources/appengine-endpoints</directory>
                    <!-- the list has a default value of ** -->
                    <includes>
                        <include>WEB-INF/*.discovery</include>
                        <include>WEB-INF/*.api</include>
                    </includes>
                </resource>
            </webResources>
        </configuration>
    </plugin>
    
  2. Locate and replace the default appengine-maven-plugin configuration entirely with the following:

    <plugin>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-maven-plugin</artifactId>
        <version>${appengine.target.version}</version>
        <configuration>
            <enableJarClasses>false</enableJarClasses>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>endpoints_get_discovery_doc</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
  3. Save your work and close the file.

  4. Rebuild the project to make sure everything is working as expected, as described in the next section.

Rebuilding the project with the new pom.xml settings

When you finish modifying the pom.xml file, rebuild the project by invoking Maven as follows:

mvn install

Next...

At this point, you are ready to write backend code.

Continue to Writing the API: A Simple GET.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.