老实说本来这是一篇简单的RESTful的入门demo,和jetty无关,但是为了方便,用到了jetty的maven插件。勉勉强强算是和jetty有关吧。
项目地址: https://github.com/adyliu/jetty-rest-demo
需要特别注意的是,从jetty7.5.3开始就必须用maven 3了,以前使用的maven 2不能使用了,就为了这问题,我跟踪了很久,大囧。
这里使用jetty最新的maven插件,同样会启动最新的jetty8.1.1 来测试。
另外也没有使用servlet 3.0的配置。
包含三部分:要扫描的住解包,mvc注解驱动以及jsp的渲染映射(其实这个例子中没有用到)。
直接返回字符串,另外将索引页映射到一个字符串上。(也不对中文进行处理)
好了,这算是一个最简单的入门例子了。
下面的参考资源中有git的源码。
项目地址: https://github.com/adyliu/jetty-rest-demo
文件列表
包含如下文件:
- git忽略文件
- README文件
- pom文件
- 一个简单的Controller文件
- 一个log4j的配置文件
- 一个简单的spring mvc配置
- 一个简单的web.xml
Maven配置
为了能够方便从jetty:run来启动web容器,使用jetty的maven插件。需要特别注意的是,从jetty7.5.3开始就必须用maven 3了,以前使用的maven 2不能使用了,就为了这问题,我跟踪了很久,大囧。
这里使用jetty最新的maven插件,同样会启动最新的jetty8.1.1 来测试。
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.1.v20120215</version>
</plugin>
</plugins>
</build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.1.v20120215</version>
</plugin>
</plugins>
</build>
web.xml
这是一个简单的web.xml配置,主要配置spring servlet。当然这里也辅助配置了一个log4j,方便查看日志输出,不配置也没关系。另外也没有使用servlet 3.0的配置。
<?
xml version="1.0" encoding="UTF-8"
?>
< web-app xmlns ="http://java.sun.com/xml/ns/j2ee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version ="2.4" >
< display-name >jetty-rest-demo </ display-name >
< context-param >
< param-name >log4jConfigLocation </ param-name >
< param-value >classpath:log4j.properties </ param-value >
</ context-param >
< listener >
< listener-class >org.springframework.web.util.Log4jConfigListener </ listener-class >
</ listener >
< servlet >
< servlet-name >dispatcher </ servlet-name >
< servlet-class >org.springframework.web.servlet.DispatcherServlet </ servlet-class >
< load-on-startup >1 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name >dispatcher </ servlet-name >
< url-pattern >/* </ url-pattern >
</ servlet-mapping >
</ web-app >
< web-app xmlns ="http://java.sun.com/xml/ns/j2ee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version ="2.4" >
< display-name >jetty-rest-demo </ display-name >
< context-param >
< param-name >log4jConfigLocation </ param-name >
< param-value >classpath:log4j.properties </ param-value >
</ context-param >
< listener >
< listener-class >org.springframework.web.util.Log4jConfigListener </ listener-class >
</ listener >
< servlet >
< servlet-name >dispatcher </ servlet-name >
< servlet-class >org.springframework.web.servlet.DispatcherServlet </ servlet-class >
< load-on-startup >1 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name >dispatcher </ servlet-name >
< url-pattern >/* </ url-pattern >
</ servlet-mapping >
</ web-app >
dispatcher-servlet.xml
接下来是spring mvc的配置。包含三部分:要扫描的住解包,mvc注解驱动以及jsp的渲染映射(其实这个例子中没有用到)。
<?
xml version="1.0" encoding="UTF-8"
?>
< beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context ="http://www.springframework.org/schema/context" xmlns:mvc ="http://www.springframework.org/schema/mvc"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd" >
< context:component-scan base-package ="info.imxylz.study.jetty.rest" />
< mvc:annotation-driven />
< bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name ="prefix" value ="/WEB-INF/pages/" ></ property >
< property name ="suffix" value =".jsp" ></ property >
< property name ="viewClass" value ="org.springframework.web.servlet.view.JstlView" ></ property >
</ bean >
</ beans >
< beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context ="http://www.springframework.org/schema/context" xmlns:mvc ="http://www.springframework.org/schema/mvc"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd" >
< context:component-scan base-package ="info.imxylz.study.jetty.rest" />
< mvc:annotation-driven />
< bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name ="prefix" value ="/WEB-INF/pages/" ></ property >
< property name ="suffix" value =".jsp" ></ property >
< property name ="viewClass" value ="org.springframework.web.servlet.view.JstlView" ></ property >
</ bean >
</ beans >
Controller
下载开始写第一个Controller,当然这个Controller稍微有一点点别扭。直接返回字符串,另外将索引页映射到一个字符串上。(也不对中文进行处理)
package info.imxylz.study.jetty.rest.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* a rest demo (spring 3.x)
*
* @author adyliu (imxylz@gmail.com)
* @since 2012-3-9
*/
@Controller
public class DemoController {
@ResponseBody
@RequestMapping("/index/{name}/")
public String index(@PathVariable("name") String name) {
return "Welcome, " + name;
}
@ResponseBody
@RequestMapping("")
public String index() {
return "This is a rest demo";
}
}
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* a rest demo (spring 3.x)
*
* @author adyliu (imxylz@gmail.com)
* @since 2012-3-9
*/
@Controller
public class DemoController {
@ResponseBody
@RequestMapping("/index/{name}/")
public String index(@PathVariable("name") String name) {
return "Welcome, " + name;
}
@ResponseBody
@RequestMapping("")
public String index() {
return "This is a rest demo";
}
}
Access
现在该是打开浏览器显示下了。
http://localhost:8080/index/Ady/
http://localhost:8080/index/Ady%20Liu/
http://localhost:8080/
http://localhost:8080/index/Ady%20Liu/
http://localhost:8080/
好了,这算是一个最简单的入门例子了。
下面的参考资源中有git的源码。
Resources
- Source code: git@github.com:adyliu/jetty-rest-demo.git (branch: v1.0-helloworld)
- Jetty Maven Plugin: http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin
- Spring MVC: http://static.springsource.org/spring/docs/3.1.1.RELEASE/spring-framework-reference/html/mvc.html