このチュートリアルでは、Google App Engine(

GAE

)環境で

Spring 3.0 MVC REST

Webアプリケーションの開発とデプロイ方法を説明します。

使用されるツールと技術:

  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

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

Eclipseで、 ”

SpringMVCGoogleAppEngine

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


春の新しいWebアプリケーション、title = "gae-spring-example1"、width = 354、height = 479



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

“フォルダにコピーして入れてください。


spring-dependency library、title = "gae-spring-library-example2"、width = 553、height = 518

それをあなたのプロジェクトのビルドパスにも追加してください – プロジェクトフォルダを右クリックし、 ”

Properties

“を選択してください。 ”

Java Build Path

” – > ”

Libraries

“タブを選択し、 ”

Add Jars

“ボタンをクリックし、上記のjarファイルを選択します。


spring-java-build-example3

、width = 629、高さ= 480]

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

3.1自動生成された `SpringMVCGoogleAppEngineServlet.java`を削除します。これは必要ありません。

3.2 Beanを作成し、REST構造でコントローラとして動作させます。さらに、 “message”プロパティにメッセージをDIします。


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ページ

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


File:war/list.jsp

<html>
<body>
    <h1>GAE + Spring 3 MVC REST example</h1>

    <h2>Movie : ${movie} , DI message : ${message}</h2>
</body>
</html>

5.春の設定

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


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

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


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.ディレクトリ構造

最終的なディレクトリ構造を確認してください。


gae spring最終ディレクトリ構造、title = "gae-spring-final-directory-example4"、width = 602、高さ= 431

8.ローカルで実行する

プロジェクトを右クリックし、「

Web Application

」として実行します。


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


ローカル展開環境でのspring deploy、title = "gae-spring-local-output-example5"、width = 632、高さ= 382

9. GAEに展開する

`appengine-web.xml`ファイルを更新し、あなたのApp Engineアプリケーション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>

プロジェクトを選択し、Googleのアイコン「

App Engineへのデプロイ

」をクリックします。


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


制作環境でのSpringのデプロイ、title = "gae-spring-production-example6"、width = 632、height = 382

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

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

ダウンロード:

SpringMVC-GoogleAppEngine.zip

(12 KB)

参考文献

3.0豆参照]。

http://en.wikipedia.org/wiki/Representational


state

transfer[REST

ウィキペディアを説明します]。 link://google-app-engine/google-app-engine-hello-world-example-using-eclipse[Google

App Engine + Java + Google Plugin for Eclipseの例]。リンク://spring3/spring-3-mvc-hello-world-example/[Spring 3 MVC hello

世界の例]。リンク://spring-mvc/spring-3-rest-hello-world-example/[Spring 3 REST

こんにちは世界の例]。

https://developers.google.com/appengine/docs/java/overview

[Google Add

Engine Java doc]