Restlet 第一步

内容提纲

本文创建了一个“Hello world”的资源,它将会让你在10分钟之内领略到Restlet框架的简洁。

1. 我需要做什么

2. “Helloworld” application

3. Servlet容器中运行

4. 作为独立的java程序运行

5. 总结

我需要做什么

我们假设你有一个搭建好的可用开发环境,并且你已经安装了JRE1.5或更高版本。如果你还没有下载Restlet,那么你可以从Restlet framework2.0 的最近版本中选择并下载。

“Hello, world” application

首先,让我们从REST应用的核心开始:resource。这个例子比较简单,只定义了一个resource,代码如下。

package firstSteps; import org.restlet.resource.Get; import org.restlet.resource.ServerResource; /** * Resource which has only one representation. */ public class HelloWorldResource extends ServerResource { @Get public String represent() { return "hello, world"; } } 然后,创建一个简单的application,我们将它命名为”FirstStepsApplication”

package firstSteps; import org.restlet.Application; import org.restlet.Restlet; import org.restlet.routing.Router; public class FirstStepsApplication extends Application { /** * Creates a root Restlet that will receive all incoming calls. */ @Override public Restlet createRoot() { // Create a router Restlet that routes each call //to a new instance of HelloWorldResource. Router router = new Router(getContext()); // Defines only one route router.attach("/hello", HelloWorldResource.class); return router; } }

Servlet容器中运行

因为你可能对Servlet更熟悉,我们建议在你的Servlet容器中运行这个Restlet应用程序。像往常一样,新建一个Servlet Web 应用,添加一个”firstStepsServlet”包并把上面的resourceapplication类放里边,并把下面所列的jar包导入到类库中(/WEB-INF/lib):

  • org.restlet.jar
  • org.restlet.ext.servlet_2.5.jar

然后,修改web.xml的配置信息,内容如下所示:

 

<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" 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"> <display-name>first steps servlet</display-name> <!-- Application class name --> <context-param> <param-name>org.restlet.application</param-name> <param-value>firstSteps.FirstStepsApplication</param-value> </context-param> <!-- Restlet adapter --> <servlet> <servlet-name>RestletServlet</servlet-name> <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class> </servlet> <!-- Catch all requests --> <servlet-mapping> <servlet-name>RestletServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>

 

最后,将整个工程打包为WAR文件,可以命名为firstStepsServlet.war,并将其部署在你的servlet容器中。容器启动后,打开浏览器,输入URL http://<你的服务器名字>:<服务器端口>/firstStepsServlet,你将在页面上看到”Hello, world”

你可以下载这个war文件http://www.restlet.org/documentation/2.0/examples/firstSteps/sources.zip

作为独立的java文件运行

一个Restlet应用程序不仅能够运行在Servlet容器中,也能通过引用jar”org.restlet.jar”来作为独立的java文件运行。下面的这段代码创建了一个主类,在其中定义了一个新的HTTP服务器,来监听端口8182并代理所有向FirstStepsApplication发来的请求。

 

public static void main(String[] args) { try { // Create a new Component. Component component = new Component(); // Add a new HTTP server listening on port 8182. component.getServers().add(Protocol.HTTP, 8182); // Attach the sample application. component.getDefaultHost().attach(new FirstStepsApplication()); // Start the component. component.start(); } catch (Exception e) { // Something is wrong. e.printStackTrace(); } }

运行主类后,在浏览器中输入链接:http://localhost:8182/hello,服务器将会显示“hello, world”。另外,要确保classpath是正确的并且端口8182没有被其它程序占用。

总结

但愿这些简单的流程能对你有所帮助,但愿能继续学习first Resource或者进行更深一步的学习

 

相关资源:

http://www.restlet.org/documentation/2.0/firstSteps

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值