Google App Engine + Spring 3 MVC RESTの例

Google App Engine + Spring 3 MVC RESTの例

このチュートリアルでは、Google App Engine(GAE)環境でSpring 3.0 MVC RESTWebアプリケーションを開発およびデプロイする方法を示します。

使用されるツールとテクノロジー:

  1. Google App Engine Java SDK 1.6.3.1

  2. 春3.1.1

  3. JDK 1.6

  4. Eclipse 3.7 + Eclipse用Googleプラグイン

Note
この例では、このSpring 3 MVC REST exampleを再利用し、変更してGoogle App Engineと統合します。これを読むこともできます–GAE + Java + Eclipse example

1. 新しいWebアプリケーションプロジェクト

Eclipseで、「SpringMVCGoogleAppEngine」という名前の新しいWebアプリケーションプロジェクトを作成します。

gae spring new web application

Google Plugin for Eclipse」は、GAEプロジェクト構造のサンプルを生成します。

2. Spring 3.0の依存関係

GAEでSpring MVC + RESTを使用するには、次のjarファイルが必要です。

  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

コピーして「war/WEB-INF/lib」フォルダに入れます。

gae spring dependency library

プロジェクトのビルドパスにも追加します。プロジェクトフォルダを右クリックして、「Properties」を選択します。 「Java Build Path」→「Libraries」タブを選択し、「Add Jars」ボタンをクリックして、上のjarファイルを選択します。

gae spring java build path

3. スプリングコントローラー

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

3.2 Create a bean, act as controller in REST structure. さらに、メッセージを「message」プロパティにDIします。

ファイル:src / com / example / MovieController.java

package com.example.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ページ

list.jspページを作成し、結果を表示します。

ファイル:war / list.jsp



    

GAE + Spring 3 MVC REST example

Movie : ${movie} , DI message : ${message}

5. スプリング構成

Spring XML Bean構成ファイルを作成し、Beanを定義してリゾルバーを表示します。

ファイル:war / WEB-INF / mvc-dispatcher-servlet.xml



    
    
        
    

    

    
    
        
            Hello World
        
    

    
        
            /pages/
        
        
            .jsp
        
    

6. web.xml

web.xmlを更新し、Springフレームワークを統合します。

ファイル:war / WEB-INF / web.xml



    
        mvc-dispatcher
        
                    org.springframework.web.servlet.DispatcherServlet
                
        1
    

    
        mvc-dispatcher
        /
    

    
        contextConfigLocation
        /WEB-INF/mvc-dispatcher-servlet.xml
    

    
        
                    org.springframework.web.context.ContextLoaderListener
                
    

    
        index.html
    

7. ディレクトリ構造

最終的なディレクトリ構造を確認します。

gae spring final directory structure

8. ローカルで実行

プロジェクトを右クリックし、「Web Application」として実行します。

gae spring deploy on local development environemnt

9. GAEにデプロイする

appengine-web.xmlファイルを更新し、AppEngineアプリケーションIDを追加します。

ファイル:war / WEB-INF / appengine-web.xml


  example-springmvc
  1

  
    
  

プロジェクトを選択し、Googleアイコン「Deploy to App Engine」をクリックします。

gae spring deploy on production environment

ソースコードをダウンロード

ファイルサイズが大きいため、すべてのSpringおよびGAE jarは除外されます。

ダウンロード–SpringMVC-GoogleAppEngine.zip(12 KB)