在那篇文章中,開發了一個簡單的Rest示例。 為了測試該應用程序,將文件復制到Web服務器(例如Tomcat )中,然后返回對字符1的http:// localhost:8080 / RestServer / characters / 1信息的訪問。
在當前文章中,我將解釋如何將應用程序轉換為Google App Engine并使用Maven部署到Google的基礎架構中。 當然,在這種情況下,我們將部署Rest Spring MVC應用程序,但是可以使用相同的方法將Spring MVC Web應用程序(或使用其他Web框架開發的任何其他應用程序)遷移到GAE。
首先,顯然,您應該創建一個Google帳戶并注冊一個新的應用程序 (記住名稱,因為它將在下一步中使用)。 之后,您可以開始遷移。
需要進行三個更改,創建定義應用程序名稱的appengine-web.xml ; 使用Google帳戶信息將服務器標記添加到settings.xml,并修改pom.xml以添加GAE插件及其依賴項。
讓我們從appengine-web.xml開始。 GAE使用此文件來配置應用程序,并將其創建到WEB-INF目錄(與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>
最重要的字段是應用程序標簽。 此標記包含我們應用程序的名稱(在您注冊新的Google應用程序時定義)。
其他標簽包括版本,系統屬性和環境變量以及其他配置,例如您是否希望預編譯以提高性能或應用程序需要會話。
而且您的項目不再需要修改,現在僅會觸摸Maven文件。
在settings.xml中 ,應該添加帳戶信息:
<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>
看到這和在Maven中注冊任何其他服務器一樣容易。
最后是最繁瑣的部分,修改pom.xml 。
首先要添加新屬性:
<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>
在第一行中,我們定義了Appengine Java SDK的位置。 如果已經安裝,則在此標記中插入位置,如果沒有,則復制此pom的相同位置,然后將maven存儲庫目錄(在我的情況下為/ media / share / maven_repo)更改為您的目錄。 通常,您的Maven存儲庫位置為/home/user/.m2/repositories 。 Maven將在部署時為您下載SDK 。
下一步是添加Maven 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>
因為我們的項目是虛擬項目, 所以不使用Datanucleus 。 對于更復雜的項目,需要使用例如JDO進行數據庫訪問,應添加下一個依賴項:
<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>
如果您使用的是Datanucleus ,則應注冊maven-datanucleus-plugin 。 請注意根據您的項目正確配置它。
<plugin><groupId>org.datanucleus</groupId><artifactId>maven-datanucleus-plugin</artifactId><version>1.1.4</version><configuration><!--Make sure this path contains your persistentclasses!--><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>
現在,添加了Google App Engine依賴項。
<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>
然后,如果您想測試GAE 功能 (在我們的虛擬項目中未使用),則添加下一個GAE庫:
<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>
接下來的更改是對maven-war-plugin的修改,其中將appengine-web.xml包含到生成的包中:
<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>
最后添加maven-gae-plugin并將其配置為將應用程序上傳到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>
看到<serviceId>標記包含先前在settings.xml文件中定義的服務器名稱。
另外,如果您使用的是maven-release-plugin ,則可以在release:perform目標期間將應用程序自動上傳到appspot :
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-release-plugin</artifactId><version>2.2.1</version><configuration><goals>gae:deploy</goals></configuration>
</plugin>
現在運行gae:deploy目標。 如果您已經安裝了Appengine Java SDK ,那么您的應用程序將被上傳到您的GAE網站。 但是,如果這是您第一次運行該插件,則會收到錯誤消息。 不要驚慌,發生此錯誤是因為Maven插件未在您在<gae.home>標記中指定的目錄中找到Appengine SDK 。 但是,如果您已將gae.home位置配置到本地Maven存儲庫中,只需運行gae:unpack目標,即可正確安裝SDK ,因此當您重新運行gae:deploy時,您的應用程序將上傳到Google基礎架構中。
在后例子中,你可以去http://alexsotoblog.appspot.com/characters/1http://alexsotoblog.appspot.com/characters/1和JSON格式的字符信息顯示到瀏覽器中。
正如我在文章開頭提到的那樣,相同的過程可以用于任何Web應用程序,而不僅僅是Spring Rest MVC 。
由于教學目的,對應用程序pom進行了所有修改。 我的建議是,您要使用GAE相關標簽創建父pom ,因此必須上傳到Google App Engine的每個項目都來自同一pom文件。
我希望您發現這篇文章有用。
本周,我在devoxx ,在那與我見面;)我將在17日(星期四)13:00發表有關通過聚合和最小化加速Javascript和CSS下載時間的演講 。
完整的pom文件:
<?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>
下載代碼。
音樂: http : //www.youtube.com/watch?v = Nba3Tr_GLZU
參考:來自JCG合作伙伴 Alex Soto 在Google App Engine上的Spring MVC和REST,來自One Jar To Rule All All博客。
相關文章 :
- 使用Spring MVC開發Restful Web服務
- Spring MVC開發–快速教程
- jqGrid,REST,AJAX和Spring MVC集成
- 使用Spring 3.1和基于Java的配置構建RESTful Web服務,第2部分
- Spring MVC3 Hibernate CRUD示例應用程序
- Google AppEngine(GAE)中的多租戶
翻譯自: https://www.javacodegeeks.com/2011/12/spring-mvc-and-rest-at-google-app.html