About Alex Soto

Spring MVC and REST at Google App Engine

Some time ago I wrote about how to implement your Restful Web API using Spring MVC. Read my previous post to know about it.

In that post it was developed a simple Rest example. For testing the application, file was copied into a web server (Tomcat for example), and then accessing to http://localhost:8080/RestServer/characters/1 information of character 1 was returned.

In current post I am going to explain how to transform that application to a Google App Engine and be deployed into Google’s infrastructure using Maven. Of course in this case we are going to deploy a Rest Spring MVC application, but same approach can be used for migrating a Spring MVC web application (or any other application developed with other web framework) to GAE.

First of all, obviously you should create a Google Account and register a new application (remember the name because will be used in next step). After that you can start the migration.

Three changes are required, create appengine-web.xml defining application name; add server tag to settings.xml with Google account information, and modify pom.xml for adding GAE plugin and its dependencies.

Let’s start with appengine-web.xml. This file is used by GAE to configure application and is created into WEB-INF directory (at same level of web.xml).

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://appengine.google.com/ns/1.0 http://googleappengine.googlecode.com/svn/branches/1.2.1/java/docs/appengine-web.xsd">
    <application>alexsotoblog</application>
    <version>1</version>

    <system-properties>
        <property name="java.util.logging.config.file" value="WEB-INF/classes/logging.properties"/>
    </system-properties>
    <precompilation-enabled>false</precompilation-enabled>
    <sessions-enabled>true</sessions-enabled>
</appengine-web-app>

The most important field is application tag. This tag contains the name of our application (defined when you register a new Google Application).

Other tags are version, system properties and environment variables, and misc configuration like if you want a precompilation to enhance performance or if your application requires sessions.

And your project should not be modified anymore, now only Maven files will be touched.

In settings.xml, account information should be added:

<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
  <localRepository>/media/share/maven_repo</localRepository>
  <servers>
        <server>
            <id>appengine.google.com</id>
            <username>my_account@gmail.com</username>
            <password>my_password</password>
        </server>

    </servers>
  
</settings>

See that it is as easy as registering any other server in Maven.

And finally the most tedious part, modifying pom.xml.

First thing is adding new properties:

<gae.home>/media/share/maven_repo/com/google/appengine/appengine-java-sdk/1.5.5/appengine-java-sdk-1.5.5</gae.home>
<gaeApplicationName>alexsotoblog</gaeApplicationName>
<gaePluginVersion>0.9.0</gaePluginVersion>
<gae.version>1.5.5</gae.version>

<!-- Upload to http://test.latest.<applicationName>.appspot.com by default -->
<gae.application.version>test</gae.application.version>

At first line we are defining Appengine Java SDK location. If you have already installed then insert location in this tag, if not, copy same location of this pom and simply change maven repository directory, in my case /media/share/maven_repo, to yours. Typically your Maven repository location will be /home/user/.m2/repositories. Maven will download SDK for you at deploy time.

Next step is adding Maven GAE repository.

<repositories>
 <repository>
  <id>maven-gae-plugin-repo</id>
  <url>http://maven-gae-plugin.googlecode.com/svn/repository</url>
  <name>maven-gae-plugin repository</name>
 </repository>
</repositories>

<pluginRepositories>
 <pluginRepository>
  <id>maven-gae-plugin-repo</id>
  <name>Maven Google App Engine Repository</name>
  <url>http://maven-gae-plugin.googlecode.com/svn/repository/</url>
 </pluginRepository>
</pluginRepositories>

Because our project is dummy project, Datanucleus are not used. In case of more complex projects, that database access is required using, for example JDO, next dependencies should be added:

<dependency>
 <groupId>javax.jdo</groupId>
 <artifactId>jdo2-api</artifactId>
 <version>2.3-eb</version>
 <exclusions>
  <exclusion>
   <groupId>javax.transaction</groupId>
   <artifactId>transaction-api</artifactId>
  </exclusion>
 </exclusions>
</dependency>

<dependency>
 <groupId>com.google.appengine.orm</groupId>
 <artifactId>datanucleus-appengine</artifactId>
 <version>1.0.6.final</version>
</dependency>

<dependency>
 <groupId>org.datanucleus</groupId>
 <artifactId>datanucleus-core</artifactId>
 <version>1.1.5</version>
 <scope>runtime</scope>
 <exclusions>
  <exclusion>
   <groupId>javax.transaction</groupId>
   <artifactId>transaction-api</artifactId>
  </exclusion>
 </exclusions>
</dependency>

<dependency>
 <groupId>com.google.appengine</groupId>
 <artifactId>geronimo-jta_1.1_spec</artifactId>
 <version>1.1.1</version>
 <scope>runtime</scope>
</dependency>


<dependency>
 <groupId>com.google.appengine</groupId>
 <artifactId>geronimo-jpa_3.0_spec</artifactId>
 <version>1.1.1</version>
 <scope>runtime</scope>
</dependency>

And in case you are using Datanucleus, maven-datanucleus-plugin should be registered. Take care to configure it properly depending on your project.

<plugin>
            <groupId>org.datanucleus</groupId>
            <artifactId>maven-datanucleus-plugin</artifactId>
            <version>1.1.4</version>
            <configuration>
                <!--
                    Make sure this path contains your persistent
                    classes!
                -->
                <mappingIncludes>**/model/*.class</mappingIncludes>
                <verbose>true</verbose>
                <enhancerName>ASM</enhancerName>
                <api>JDO</api>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>enhance</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.datanucleus</groupId>
                    <artifactId>datanucleus-core</artifactId>
                    <version>1.1.5</version>
                    <exclusions>
                        <exclusion>
                            <groupId>javax.transaction</groupId>
                            <artifactId>transaction-api</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
                <dependency>
                    <groupId>org.datanucleus</groupId>
                    <artifactId>datanucleus-rdbms</artifactId>
                    <version>1.1.5</version>
                </dependency>
                <dependency>
                 <groupId>org.datanucleus</groupId>
                 <artifactId>datanucleus-enhancer</artifactId>
                <version>1.1.5</version>
           </dependency>
       </dependencies>
</plugin>

Now Google App Engine dependencies are added.

<dependency>
 <groupId>com.google.appengine</groupId>
 <artifactId>appengine-api-1.0-sdk</artifactId>
 <version>${gae.version}</version>
</dependency>

<dependency>
 <groupId>com.google.appengine</groupId>
 <artifactId>appengine-tools-api</artifactId>
 <version>1.3.7</version>
</dependency>

Then if you want to test GAE functionalities (not used in our dummy project), next GAE libraries are added:

<dependency>
 <groupId>com.google.appengine</groupId>
 <artifactId>appengine-api-labs</artifactId>
 <version>${gae.version}</version>
 <scope>test</scope>
</dependency>

<dependency>
 <groupId>com.google.appengine</groupId>
 <artifactId>appengine-api-stubs</artifactId>
 <version>${gae.version}</version>
 <scope>test</scope>
</dependency>

<dependency>
 <groupId>com.google.appengine</groupId>
 <artifactId>appengine-testing</artifactId>
 <version>${gae.version}</version>
 <scope>test</scope>
</dependency>

Next change is a modification on maven-war-plugin including appengine-web.xml into generated package:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-war-plugin</artifactId>
 <configuration>
  <webResources>
   <resource>
    <directory>src/main/webapp</directory>
    <filtering>true</filtering>
    <includes>
     <include>**/appengine-web.xml</include>
    </includes>
   </resource>
  </webResources>
 </configuration>
</plugin>

And finally adding maven-gae-plugin and configuring it to upload application to appspot.

<plugin>
 <groupId>net.kindleit</groupId>
 <artifactId>maven-gae-plugin</artifactId>
 <version>${gaePluginVersion}</version>
 <configuration>
  <serverId>appengine.google.com</serverId>
 </configuration>
 <dependencies>
  <dependency>
   <groupId>net.kindleit</groupId>
   <artifactId>gae-runtime</artifactId>
   <version>${gae.version}</version>
   <type>pom</type>
  </dependency>
 </dependencies>
</plugin>

See that <serviceId> tag contains the server name defined previously in settings.xml file.

Also if you are using maven-release-plugin you can upload application to the appspot automatically, during release:perform goal:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-release-plugin</artifactId>
        <version>2.2.1</version>
        <configuration>
              <goals>gae:deploy</goals>
        </configuration>
</plugin>

Now run gae:deploy goal. If you have already installed Appengine Java SDK, then your application will be uploaded to your GAE site. But if it is the first time you run the plugin, you will receive an error. Do not panic, this error occurs because Maven plugin does not find Appengine SDK into directory you specified in <gae.home> tag. But if you have configured gae.home location into your local Maven repository, simply run gae:unpack goal, and SDK will be installed correctly so when you rerun gae:deploy your application will be uploaded into Google infrastructure.

In post example you can go to http://alexsotoblog.appspot.com/characters/1http://alexsotoblog.appspot.com/characters/1 and character information in JSON format is displayed into your browser.

As I have noted at the beginning of the post, the same process can be used for any web application, not only for Spring Rest MVC.

Because of teaching purpose all modifications have been made into application pom. My advice is that you create a parent pom with GAE related tags, so each project that must be uploaded into Google App Engine extends from same pom file.

I wish you have found this post useful.

This week I am at devoxx, meet me there ;) I will be speaking on Thursday 17 at 13:00 about Speeding Up Javascript & CSS Download Times With Aggregation and Minification.

Full pom file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>org.springframework</groupId>
 <artifactId>rest</artifactId>
 <name>Rest</name>
 <packaging>war</packaging>
 <version>1.0.0-BUILD-SNAPSHOT</version>
 <properties>
  <java-version>1.6</java-version>
  <org.springframework-version>3.0.4.RELEASE</org.springframework-version>
  <org.aspectj-version>1.6.9</org.aspectj-version>
  <org.slf4j-version>1.5.10</org.slf4j-version>
  <!-- Specify AppEngine version for your project. It should match SDK version 
   pointed to by ${gae.home} property (Typically, one used by your Eclipse plug-in) -->

  <gae.home>/home/alex/.m2/repository/com/google/appengine/appengine-java-sdk/1.5.5/appengine-java-sdk-1.5.5</gae.home>
  <gaeApplicationName>alexsotoblog</gaeApplicationName>
  <gaePluginVersion>0.9.0</gaePluginVersion>
  <gae.version>1.5.5</gae.version>

  <!-- Upload to http://test.latest.<applicationName>.appspot.com by default -->
  <gae.application.version>test</gae.application.version>
 </properties>
 <dependencies>

  <!-- Rest -->
  <dependency>
   <groupId>com.sun.xml.bind</groupId>
   <artifactId>jaxb-impl</artifactId>
   <version>2.2.4-1</version>
  </dependency>
  <dependency>
   <groupId>org.codehaus.jackson</groupId>
   <artifactId>jackson-core-lgpl</artifactId>
   <version>1.8.5</version>
  </dependency>
  <dependency>
   <groupId>org.codehaus.jackson</groupId>
   <artifactId>jackson-mapper-lgpl</artifactId>
   <version>1.8.5</version>
  </dependency>

  <!-- GAE libraries for local testing as described here: http://code.google.com/appengine/docs/java/howto/unittesting.html -->
  <dependency>
   <groupId>com.google.appengine</groupId>
   <artifactId>appengine-api-labs</artifactId>
   <version>${gae.version}</version>
   <scope>test</scope>
  </dependency>

  <dependency>
   <groupId>com.google.appengine</groupId>
   <artifactId>appengine-api-stubs</artifactId>
   <version>${gae.version}</version>
   <scope>test</scope>
  </dependency>

  <dependency>
   <groupId>com.google.appengine</groupId>
   <artifactId>appengine-testing</artifactId>
   <version>${gae.version}</version>
   <scope>test</scope>
  </dependency>


  <dependency>
   <groupId>com.google.appengine</groupId>
   <artifactId>appengine-api-1.0-sdk</artifactId>
   <version>${gae.version}</version>
  </dependency>

  <dependency>
   <groupId>com.google.appengine</groupId>
   <artifactId>appengine-tools-api</artifactId>
   <version>1.3.7</version>
  </dependency>

  <!-- Spring -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${org.springframework-version}</version>
   <exclusions>
    <!-- Exclude Commons Logging in favor of SLF4j -->
    <exclusion>
     <groupId>commons-logging</groupId>
     <artifactId>commons-logging</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${org.springframework-version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-oxm</artifactId>
   <version>${org.springframework-version}</version>
  </dependency>

  <!-- AspectJ -->
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjrt</artifactId>
   <version>${org.aspectj-version}</version>
  </dependency>

  <!-- Logging -->
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-api</artifactId>
   <version>${org.slf4j-version}</version>
  </dependency>
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>jcl-over-slf4j</artifactId>
   <version>${org.slf4j-version}</version>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>${org.slf4j-version}</version>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.15</version>
   <exclusions>
    <exclusion>
     <groupId>javax.mail</groupId>
     <artifactId>mail</artifactId>
    </exclusion>
    <exclusion>
     <groupId>javax.jms</groupId>
     <artifactId>jms</artifactId>
    </exclusion>
    <exclusion>
     <groupId>com.sun.jdmk</groupId>
     <artifactId>jmxtools</artifactId>
    </exclusion>
    <exclusion>
     <groupId>com.sun.jmx</groupId>
     <artifactId>jmxri</artifactId>
    </exclusion>
   </exclusions>
   <scope>runtime</scope>
  </dependency>

  <!-- @Inject -->
  <dependency>
   <groupId>javax.inject</groupId>
   <artifactId>javax.inject</artifactId>
   <version>1</version>
  </dependency>

  <!-- Servlet -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>2.5</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>jsp-api</artifactId>
   <version>2.1</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
  </dependency>

  <!-- Test -->
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.7</version>
   <scope>test</scope>
  </dependency>

 </dependencies>
 <repositories>
  <!-- For testing against latest Spring snapshots -->
  <repository>
   <id>org.springframework.maven.snapshot</id>
   <name>Spring Maven Snapshot Repository</name>
   <url>http://maven.springframework.org/snapshot</url>
   <releases>
    <enabled>false</enabled>
   </releases>
   <snapshots>
    <enabled>true</enabled>
   </snapshots>
  </repository>
  <!-- For developing against latest Spring milestones -->
  <repository>
   <id>org.springframework.maven.milestone</id>
   <name>Spring Maven Milestone Repository</name>
   <url>http://maven.springframework.org/milestone</url>
   <snapshots>
    <enabled>false</enabled>
   </snapshots>
  </repository>
  <!-- GAE repositories -->
  <repository>
   <id>maven-gae-plugin-repo</id>
   <url>http://maven-gae-plugin.googlecode.com/svn/repository</url>
   <name>maven-gae-plugin repository</name>
  </repository>
 </repositories>

 <pluginRepositories>
  <pluginRepository>
   <id>maven-gae-plugin-repo</id>
   <name>Maven Google App Engine Repository</name>
   <url>http://maven-gae-plugin.googlecode.com/svn/repository/</url>
  </pluginRepository>
 </pluginRepositories>

 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>${java-version}</source>
     <target>${java-version}</target>
    </configuration>
   </plugin>

   <!-- Adding appengine-web into war -->
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
     <webResources>
      <resource>
       <directory>src/main/webapp</directory>
       <filtering>true</filtering>
       <includes>
        <include>**/appengine-web.xml</include>
       </includes>
      </resource>
     </webResources>
     <warName>abc</warName>
    </configuration>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
     <execution>
      <id>install</id>
      <phase>install</phase>
      <goals>
       <goal>sources</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <!-- Have to use version 1.2 since version 1.3 does not appear to work 
     with ITDs -->
    <version>1.2</version>
    <dependencies>
     <!-- You must use Maven 2.0.9 or above or these are ignored (see MNG-2972) -->
     <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>${org.aspectj-version}</version>
     </dependency>
     <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjtools</artifactId>
      <version>${org.aspectj-version}</version>
     </dependency>
    </dependencies>
    <executions>
     <execution>
      <goals>
       <goal>compile</goal>
       <goal>test-compile</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <outxml>true</outxml>
     <source>${java-version}</source>
     <target>${java-version}</target>
    </configuration>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
     <junitArtifactName>junit:junit</junitArtifactName>
    </configuration>
   </plugin>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>tomcat-maven-plugin</artifactId>
    <version>1.0-beta-1</version>
   </plugin>
   <!-- The actual maven-gae-plugin. Type "mvn gae:run" to run project, "mvn 
    gae:deploy" to upload to GAE. -->
   <plugin>
    <groupId>net.kindleit</groupId>
    <artifactId>maven-gae-plugin</artifactId>
    <version>${gaePluginVersion}</version>
    <configuration>
     <serverId>appengine.google.com</serverId>
    </configuration>
    <dependencies>
     <dependency>
      <groupId>net.kindleit</groupId>
      <artifactId>gae-runtime</artifactId>
      <version>${gae.version}</version>
      <type>pom</type>
     </dependency>
    </dependencies>
   </plugin>
  </plugins>
 </build>
</project>

Download Code.
Music: http://www.youtube.com/watch?v=Nba3Tr_GLZU

Reference: Spring MVC and REST at Google App Engine from our JCG partner Alex Soto at the One Jar To Rule Them All blog.

Related Articles :

Related Whitepaper:

Introduction to Web Applications Development

Kick start your web apps development with this introductory ebook!

This 376 page eBook 'Introduction to Web Applications Development', starts with an introduction to the internet, including a brief history of the TCT/IP protocol and World Wide Web. It defines the basic concepts for web servers and studies the case of Apache, the most used webserver, while other free software webservers are not forgotten. It continues with webpage design focusing on HTML and JavaScript. XML Schemas, their validation and transformation are covered as well as dynamic webpages built with CGI, PHP or JSP and database access.

Get it Now!  

5 Responses to "Spring MVC and REST at Google App Engine"

  1. Ganeshji Marwaha says:

    Excellent article. Can you add how to test the GAE app in dev mode without uploading it to the server as well. 

  2. oussama zoghlami says:

    Very useful, thank you :)))

  3. Very usefull!!
    I put true into my appengine-web.xml

  4. Jayesh says:

    Hi,

    I am facing following issue can any one guide me what am I doing wrong. Struggling for almost a week. I have downloaded this example and trying to run but its not working. [ERROR] Unable to execute AppCfg

    java.lang.ExceptionInInitializerError

    at com.google.appengine.tools.util.Logging.initializeLogging(Logging.java:35)

    at com.google.appengine.tools.admin.AppCfg.main(AppCfg.java:94)

    at net.kindleit.gae.EngineGoalBase$1.run(EngineGoalBase.java:318)

    Caused by: java.lang.IllegalArgumentException: Unable to find C:Erndinstallslibshared

    at com.google.appengine.tools.info.SdkInfo._getLibs(SdkInfo.java:76)

    at com.google.appengine.tools.info.SdkInfo.getLibsRecursive(SdkInfo.java:69)

    at com.google.appengine.tools.info.SdkInfo.determineSharedLibFiles(SdkInfo.java:302)

    at com.google.appengine.tools.info.SdkInfo.init(SdkInfo.java:237)

    at com.google.appengine.tools.info.SdkInfo.getSdkRoot(SdkInfo.java:190)

    at com.google.appengine.tools.info.SdkImplInfo.(SdkImplInfo.java:19)

    … 3 more

Leave a Reply


1 × one =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books