Google App Engine + Spring 3 MVC REST example

In this tutorial, we will show you how to develop and deploy a Spring 3.0 MVC REST web application in Google App Engine (GAE) environment.

Tools and technologies used :

  1. Google App Engine Java SDK 1.6.3.1
  2. Spring 3.1.1
  3. JDK 1.6
  4. Eclipse 3.7 + Google Plugin for Eclipse
Note
This example is going to reuse this Spring 3 MVC REST example, modify it and integrate with Google App Engine, you may also interest to read this – GAE + Java + Eclipse example

1. New Web Application Project

In Eclipse, create a new Web Application project, named as “SpringMVCGoogleAppEngine“.

gae spring new web application gae spring new web application

The “Google Plugin for Eclipse” will generate a sample of GAE project structure.

2. Spring 3.0 Dependencies

To use Spring MVC + REST in GAE, you need following jars

  1. aopalliance-1.0.jar
  2. commons-logging-1.1.1.jar
  3. spring-aop-3.1.1.RELEASE.jar
  4. spring-asm-3.1.1.RELEASE.jar
  5. spring-beans-3.1.1.RELEASE.jar
  6. spring-context-3.1.1.RELEASE.jar
  7. spring-context-support-3.1.1.RELEASE.jar
  8. spring-core-3.1.1.RELEASE.jar
  9. spring-expression-3.1.1.RELEASE.jar
  10. spring-web-3.1.1.RELEASE.jar
  11. spring-webmvc-3.1.1.RELEASE.jar

Copy and put it in “war/WEB-INF/lib” folder.

gae spring dependency library gae spring dependency library

Add it to your project’s build path as well – right click on project folder, select “Properties“. Select “Java Build Path” -> “Libraries” tab, click “Add Jars” button and select above jars.

gae spring java build path gae spring java build path

3. Spring Controller

3.1 Delete auto generated SpringMVCGoogleAppEngineServlet.java, you don’t need this.

3.2 Create a bean, act as controller in REST structure. In addition, DI a message into the “message” property.

File : src/com/mkyong/MovieController.java

package com.mkyong.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller
@RequestMapping("/movie")
public class MovieController {
 
//DI via Spring
String message;
 
@RequestMapping(value="/{name}", method = RequestMethod.GET)
public String getMovie(@PathVariable String name, ModelMap model) {
 
model.addAttribute("movie", name);
model.addAttribute("message", this.message);
 
//return to jsp page, configured in mvc-dispatcher-servlet.xml, view resolver
return "list";
 
}
 
public void setMessage(String message) {
this.message = message;
}
 
}

4. JSP Pages

Create a list.jsp page, display the result.

File : war/list.jsp

<html>
<body>
<h1>GAE + Spring 3 MVC REST example</h1>
 
<h3>Movie : ${movie} , DI message : ${message}</h3>
</body>
</html>

5. Spring Configuration

Create a Spring XML bean configuration file, define the beans and view resolver.

File : war/WEB-INF/mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

 
<!--
Need DI a message into controller, so auto component scan is disabled,
to avoid double create the movieController bean.
Only controller need this hack.
-->
<context:component-scan base-package="com.mkyong.controller">
<context:exclude-filter type="regex"
expression="com.mkyong.controller.Movie.*" />
</context:component-scan>
 
<mvc:annotation-driven />
 
<!-- Bean to show you Di in GAE, via Spring, also init the MovieController -->
<bean class="com.mkyong.controller.MovieController">
<property name="message">
<value>Hello World</value>
</property>
</bean>
 
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
 
</beans>

6. web.xml

Update web.xml, integrate Spring framework.

File : war/WEB-INF/web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

 
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
 
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
 
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
 
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

7. Directory Structure

Review the final directory structure.

gae spring final directory structure gae spring final directory structure

8. Run on Local

Right click on the project, run as “Web Application“.

URL : http://localhost:8888/movie/Avengers

gae spring deploy on local development environemnt gae spring deploy on local development environemnt

9. Deploy on GAE

Update appengine-web.xml file, add your App Engine application ID.

File : war/WEB-INF/appengine-web.xml

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>mkyong-springmvc</application>
<version>1</version>
 
<system-properties>
<property name="java.util.logging.config.file"
value="WEB-INF/logging.properties"/>
</system-properties>
 
</appengine-web-app>

Select project, click on Google icon, “Deploy to App Engine“.

URL : http://mkyong-springmvc.appspot.com/movie/forrest%20gump

gae spring deploy on production environment gae spring deploy on production environment

Download Source Code

Due to large file size, all Spring and GAE jars are excluded.

Download – SpringMVC-GoogleAppEngine.zip (12 KB)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s