xylz,imxylz

          關注后端架構、中間件、分布式和并發編程

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            111 隨筆 :: 10 文章 :: 2680 評論 :: 0 Trackbacks
          老實說本來這是一篇簡單的RESTful的入門demo,和jetty無關,但是為了方便,用到了jetty的maven插件。勉勉強強算是和jetty有關吧。

          項目地址: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>

          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>

          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>

          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";
              }
          }

          Access

          現在該是打開瀏覽器顯示下了。
          http://localhost:8080/index/Ady/
          http://localhost:8080/index/Ady%20Liu/
          http://localhost:8080/

          好了,這算是一個最簡單的入門例子了。
          下面的參考資源中有git的源碼。

          Resources



          ©2009-2014 IMXYLZ |求賢若渴
          posted on 2012-03-09 17:52 imxylz 閱讀(8469) 評論(0)  編輯  收藏 所屬分類: Jetty

          ©2009-2014 IMXYLZ
          主站蜘蛛池模板: 宜兰县| 兴化市| 绥芬河市| 涞源县| 安远县| 东台市| 增城市| 苗栗县| 高要市| 蓝田县| 南开区| 呼玛县| 丰宁| 深泽县| 河津市| 巴林左旗| 邢台县| 伊吾县| 仁布县| 西峡县| 南通市| 晴隆县| 百色市| 洮南市| 彭州市| 金湖县| 轮台县| 新余市| 新竹县| 常宁市| 民乐县| 安图县| 师宗县| 和林格尔县| 都昌县| 兴业县| 高州市| 天津市| 阿巴嘎旗| 丽水市| 金溪县|