Google App Engine + Spring 3 MVC REST示例

在本教程中,我们将向您展示如何在Google App Engine( GAE )环境中开发和部署Spring 3.0 MVC REST Web应用程序。

使用的工具和技术:

  1. Google App Engine Java SDK 1.6.3.1
  2. 春天3.1.1
  3. JDK 1.6
  4. Eclipse 3.7 + Eclipse的Google插件

注意
此示例将重用此Spring 3 MVC REST示例 ,对其进行修改并与Google App Engine集成,您可能还想阅读该示例GAE + Java + Eclipse示例

1.新的Web应用程序项目

在Eclipse中,创建一个名为“ SpringMVCGoogleAppEngine ”的新Web应用程序项目。

gae spring new web application

Eclipse的Google插件 ”将生成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. 弹簧上下文支持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 ”按钮,然后选择上方的jars。

gae spring java build path

3.弹簧控制器

3.1删除自动生成的SpringMVCGoogleAppEngineServlet.java ,您不需要这样做。

3.2创建一个bean,在REST结构中充当控制器。 另外,将一条消息放入DI的“ message ”属性中。

文件: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页面,显示结果。

文件: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并查看解析器。

文件: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框架。

文件: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 final directory structure

8.在本地运行

右键单击该项目,以“ Web应用程序 ”运行。

网址:http:// localhost:8888 / movie / Avengers

gae spring deploy on local development environemnt

9.在GAE上部署

更新appengine-web.xml文件,添加您的App Engine应用程序ID。

文件: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图标“ Deploy to App Engine ”。

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

gae spring deploy on production environment

下载源代码

由于文件较大,因此不包括所有Spring和GAE jar。

下载– SpringMVC-GoogleAppEngine.zip (12 KB)

参考文献

  1. Spring 3.0 bean参考
  2. REST解释了维基百科
  3. Google App Engine + Java + Google Eclipse插件示例
  4. Spring 3 MVC你好世界示例
  5. Spring 3 REST Hello世界示例
  6. Google Add Engine Java文档

翻译自: https://mkyong.com/google-app-engine/google-app-engine-spring-3-mvc-rest-example/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值