restlet getting start 1

 本文的目的在于完成一个Restlet入门示例。 
首先,是一个Stand alone应用。 
然后,将其部署与Tomcat容器中。 
最后,完成Restlet与Spring的整合。 
 基本JAR 包:

 


1.按照官方教程,完成“firstSteps” 

创建一个动态Web工程RestEE,并建立firstSteps包,并复制如下代码到:HelloWorldResource.java 

Java代码    收藏代码
  1. package firstSteps;  
  2.   
  3. import org.restlet.Context;  
  4. import org.restlet.Request;  
  5. import org.restlet.Response;  
  6. import org.restlet.resource.Get;  
  7. import org.restlet.resource.ServerResource;  
  8.   
  9. /** 
  10.  * Resource which has only one representation. 
  11.  */  
  12. public class HelloWorldResource extends ServerResource {  
  13.   
  14.     @Get  
  15.     public String represent() {  
  16.         return "hello, world";  
  17.     }  
  18.   
  19. }  


2.复制如下代码到FirstStepsApplication.java  
Java代码    收藏代码
  1. package firstSteps;  
  2.   
  3. import org.restlet.Application;  
  4. import org.restlet.Restlet;  
  5. import org.restlet.routing.Router;  
  6.   
  7. public class FirstStepsApplication extends Application {  
  8.   
  9.     /** 
  10.      * Creates a root Restlet that will receive all incoming calls. 
  11.      */  
  12.     @Override  
  13.     public synchronized Restlet createInboundRoot() {  
  14.         // Create a router Restlet that routes each call to a new instance of HelloWorldResource.  
  15.         Router router = new Router(getContext());  
  16.   
  17.         // Defines only one route  
  18.         router.attach("/hello", HelloWorldResource.class);  
  19.   
  20.         return router;  
  21.     }  
  22.   
  23. }  

3.编写main,复制如下代码到:FirstStepsMain.java  
Java代码    收藏代码
  1. package firstSteps;  
  2.   
  3. import org.restlet.Component;  
  4. import org.restlet.data.Protocol;  
  5.   
  6. public class FirtstStepsMain {  
  7.     public static void main(String[] args) throws Exception {    
  8.         // Create a new Component.    
  9.         Component component = new Component();    
  10.         
  11.         // Add a new HTTP server listening on port 8182.    
  12.         component.getServers().add(Protocol.HTTP, 8182);    
  13.         
  14.         // Attach the sample application.    
  15.         component.getDefaultHost().attach("/firstSteps",    
  16.                 new FirstStepsApplication());    
  17.         
  18.         // Start the component.    
  19.         component.start();    
  20.     }   
  21. }  


4.以Java Application运行FirstStepsMain,在浏览器中输入:  
http://localhost:8182/hello,将打印“hello, world”信息。  

5.修改web.xml,具体代码如下:  
Xml代码    收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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" id="WebApp_ID" version="2.5">  
  3.    
  4.    <display-name>firstSteps</display-name>  
  5.    <context-param>    
  6.       <param-name>org.restlet.application</param-name>    
  7.       <param-value>    
  8.          firstSteps.FirstStepsApplication  
  9.       </param-value>    
  10.    </context-param>  
  11.    <!-- Restlet adapter -->    
  12.    <servlet>    
  13.       <servlet-name>RestletServlet</servlet-name>   
  14.       <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>  
  15.         
  16.         
  17.    </servlet>    
  18.     
  19.    <!-- Catch all requests -->    
  20.    <servlet-mapping>    
  21.       <servlet-name>RestletServlet</servlet-name>    
  22.       <url-pattern>/*</url-pattern>    
  23.    </servlet-mapping>    
  24. </web-app>  


6.启动Tomcat,运行该项目。在浏览器中输入:  
http://localhost:8080/RestEE/hello,出现“hello, world”信息。  

7.集成restlet,spring  
7.1 修改web.xml,内容如下:  
Xml代码    收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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" id="WebApp_ID" version="2.5">  
  3.    
  4.    <display-name>firststepsservlet</display-name>  
  5.         
  6.    <context-param>    
  7.       <param-name>contextConfigLocation</param-name>    
  8.       <param-value>/WEB-INF/applicationContext.xml</param-value>    
  9.    </context-param>    
  10.       
  11.    <listener>    
  12.       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
  13.    </listener>    
  14.      
  15.    <!-- Restlet adapter -->    
  16.     <servlet>    
  17.         <servlet-name>RestletServlet</servlet-name>    
  18.         <servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class>  
  19.         <init-param>  
  20.             <param-name>org.restlet.component</param-name>     
  21.             <param-value>component</param-value>     
  22.         </init-param>  
  23.     </servlet>    
  24.     
  25.    <!-- Catch all requests -->    
  26.    <servlet-mapping>    
  27.       <servlet-name>RestletServlet</servlet-name>    
  28.       <url-pattern>/*</url-pattern>    
  29.    </servlet-mapping>    
  30. </web-app>  


7.2 application.xml内容如下:  
Xml代码    收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  5.    
  6.     <bean id="component" class="org.restlet.ext.spring.SpringComponent">  
  7.         <property name="defaultTarget" ref="application" />   
  8.     </bean>  
  9.     <bean id="application" class="firstSteps.FirstStepsApplication">  
  10.         <lookup-method name="createRoot" bean="root" />  
  11.     </bean>  
  12.     <bean id="root" class="org.restlet.ext.spring.SpringRouter">  
  13.         <property name="attachments">  
  14.             <map>  
  15.                 <entry key="/hello">  
  16.                     <bean class="org.restlet.ext.spring.SpringFinder">  
  17.                         <lookup-method name="create" bean="HelloWorldResource" />  
  18.                     </bean>  
  19.                 </entry>  
  20.             </map>  
  21.         </property>  
  22.     </bean>  
  23.     <bean id="HelloWorldResource" class="firstSteps.HelloWorldResource" scope="prototype" />  
  24. </beans>  


7.3 启动Tomcat,运行该项目。浏览器中输入:  

http://localhost:8080/RestEE/hello,出现“hello,world”信息。


其他资料 :

http://ajaxcn.iteye.com/category/64734?page=2

http://restlet.org/learn/tutorial/2.1/#/docs_2.0/2-restlet.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值