前段时间,我写了一篇关于如何使用Spring MVC实现Restful Web API的文章 。 阅读我以前的文章以了解它。

在那篇文章中,开发了一个简单的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/repositoriesMaven将在部署时为您下载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

Google App Engine上的Spring MVC和REST相关推荐

  1. app mvc框架_Google App Engine上的Spring MVC和REST

    app mvc框架 前段时间,我写了一篇关于如何使用Spring MVC实现Restful Web API的文章 . 阅读我以前的文章以了解它. 在那篇文章中,它开发了一个简单的Rest示例. 为了测 ...

  2. vaadin_5分钟内Google App Engine上的Vaadin App

    vaadin 在本教程中,您将学习如何创建第一个Vaadin Web应用程序,如何在本地AppEngine开发服务器上运行它以及如何将其部署到Google App Engine基础结构. 所有这些大约 ...

  3. 5分钟内Google App Engine上的Vaadin App

    在本教程中,您将学习如何创建第一个Vaadin Web应用程序,如何在本地AppEngine开发服务器上运行它以及如何将其部署到Google App Engine基础结构. 所有这些大约需要5到10分 ...

  4. google app engine上传程序

    上传和管理 Java 应用程序 上传到 Google App Engine Eclipse Google 插件为 Eclipse 工具栏添加了几个按钮.使用"App Engine 部署&qu ...

  5. python中ht_python – 如何在Google App Engine上正确安装ht...

    我将我的httplib2目录复制到我的GAE项目中,现在我收到以下错误: line 64, in _ssl_wrap_socket = ssl.wrap_socket 这是Google App Eng ...

  6. 谁更胜一筹:技术解析 Google App Engine 和 Amazon EC2

    最近一个潜在客户要求我们比较一下 Amazon EC2 和 Google App Engine,正好我们刚刚在 EC2 和 Google App Engine 上完成了两个相对来说规模较大的项目,因此 ...

  7. Google App Engine对抗Amazon EC2谁更牛?

    导读:笔者根据最近完成的一个项目,并帮助潜在客户比较Amazon EC2和Google App Engine,他将从三个角度来对比这两大云计算平台:技术.业务和未来发展趋势. 关键词:Amazon E ...

  8. 使用 Eclipse + PyDev 开发 Google App Engine 程序

    在Windows上开发应用程序,大多数的开发者会选择合适的IDE来辅助开发,简化一些设定.启动或测试等步骤.而在Windows 上若要开发Google App Engine的应用程式,使用Eclips ...

  9. Google App Engine JAX-RS REST服务

    在本文中,您将学习如何使用JAX-RS参考实现(Jersey)创建REST服务并将其部署在Google AppEngine上. 先决条件 对于本教程,您将需要: Google AppEngine帐户 ...

最新文章

  1. MVC之Model转Json
  2. mysql dba系统学习(12)mysql的数据文件 mysql dba系统学习(13)mysql的体系结构
  3. 乐易家智能机器人价格_安川焊接机器人价格多少钱?核心是质量好
  4. 《音乐达人秀:Adobe Audition实战200例》——实例7 定时录制网络音乐节目
  5. python计算两个日期的差
  6. .net 引用Com组件的几种方案
  7. LIDAR in Google Earth
  8. 一条SQL完成跨数据库实例Join查询
  9. JavaWeb之Servlet:Cookie 和 Session
  10. 西安Uber优步司机奖励政策(1月11日~1月17日)
  11. STM32——红外遥控器实验
  12. android8 锁屏壁纸,小米8怎么设置锁屏壁纸?小米8锁屏壁纸三种设置教程
  13. 怎么样对阿里云ECS主机进行绑定域名
  14. 使用jwt方式的接口访问
  15. 数据库基础及常用SQL语句
  16. BZOJ3838 : [Pa2013]Raper
  17. 数据解读 | 广东省内,谁才有资格做下一个深圳?
  18. vop破芙工艺-注意事项
  19. 上海高校计算机二级考纲,上海市普通高校计算机等级考试考纲
  20. 蓝桥杯练习系统-基础练习34道题解答答案全汇总(c/c++)

热门文章

  1. FAT12中,如何定位大于一个扇区(512B)的文件内容
  2. junit 测试目录_JUnit 5测试中的临时目录
  3. react 线程_React式服务中的线程本地状态可用性
  4. hystrix熔断 简介_Hystrix简介–总结
  5. input发送a.jax_JAX-RS 2.0:服务器端处理管道
  6. mysql重置增量_摆脱困境:在每种测试方法之前重置自动增量列
  7. java 泛型嵌套泛型_Java泛型简介–第6部分
  8. modbus调试时间超时_Java调试器和超时
  9. nio 读取目录所有文件_在NIO.2中使用文件和目录
  10. jpa 返回数据转换_如何使用JPA类型转换器加密数据