app mvc框架
在那篇文章中,它开发了一个简单的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 persistent
classes!
-->
<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博客。
相关文章 :
翻译自: https://www.javacodegeeks.com/2011/12/spring-mvc-and-rest-at-google-app.html
app mvc框架