猫咪最近看了些REST的东西,被搞得昏头昏脑的。就下了个RestLet框架想试试看。猫咪尝试着把RestLet的入门例子文档翻译成了中文,希望能以此加深对REST的了解。猫咪在此贴出来,希望对大家能有所帮助。猫咪翻译的不好,大家多多指教。这是目前的第二稿。猫咪今天又做了一次修改。
Restlet第一步
目录
这篇文章让你在10分钟内尝试简单的Restlet框架。告诉你如何创建一个说“hello, world”的Resource。
1.我需要什么?
2.“hello, world”应用
3.在Servlet容器中运行
4.作为一个单独的Java应用运行
5.结尾
我需要什么?
我们假设你已经有了一个可以马上使用的开发环境,并且你已经安装了JRE1.5(或更高)。如果你还没有下载Restlet,请选择最新的Restlet Framework 1.0发行版。
“hello, world”应用
让我们开始一个REST应用:“Resource”的核心。这些单一Resource代码,确定了一个简单应用。拷贝/粘贴代码到“HelloWorldResource”类中。
import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;
public class HelloWorldResource extends Resource {
public HelloWorldResource(Context context, Request request,
Response response) {
super(context, request, response);
// This representation has only one type of representation.
getVariants().add(new Variant(MediaType.TEXT_PLAIN));
}
@Override
public Representation getRepresentation(Variant variant) {
Representation representation = new StringRepresentation(
"hello, world", MediaType.TEXT_PLAIN);
return representation;
}
}
然后创建应用例子。我们创建名为“FirstStepsApplication”对象并拷贝/粘贴下面的代码:
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.Router;
public class FirstStepsApplication extends Application {
public FirstStepsApplication(Context parentContext) {
super(parentContext);
}
@Override
public synchronized 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.attachDefault(HelloWorldResource.class);
return router;
}
}
在Servlet容器中运行
你可能更熟悉Servlets,我们建议你运行Restlet应用在你喜欢的Servlet容器里。像往常一样创建一个新的Servlet应用,添加“com.firstStepsServlet”包并把Resource和Application类放进去。把下面列出的jar包放入lib目录。
org.restlet.jar
com.noelios.restlet.jar
com.noelios.restlet.ext.servlet_2.4.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>
com.firstStepsServlet.FirstStepsApplication
</param-value>
</context-param>
<!-- Restlet adapter -->
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>
com.noelios.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容器里。启动Servlet容器后,打开你喜欢的浏览器,输入URL:“http://<你的服务器名>:<端口号>/firstStepsServlet”。服务器将高兴地用“hello, world”欢迎你。
你可以直接从http://www.restlet.org/documentation/1.0/examples/firstSteps/sources.zip下载“First steps application”的war文件(已包含Restlet 1.0.7的lilb包)。
作为一个单独的Java应用运行
Restlet应用并不只能运行在Servlet容器里,也可以使用下面几个Jar包所为一个独立应用运行:
org.restlet.jar
com.noelios.restlet.jar
com.noelios.restlet.ext.simple.jar
org.simpleframework.jar
如果你想要理解后面两个Jar包的意义,你可以参考连接器(http://www.restlet.org/documentation/1.0/connectors)。
创建一个主类,拷贝/粘贴下面的代码。建立一个新的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(component.getContext()));
// Start the component.
component.start();
} catch (Exception e) {
// Something is wrong.
e.printStackTrace();
}
}
启动Main对象,打开你喜欢的浏览器,输入URL:“http://localhost:8182/”,服务器将高兴地用“hello, world”欢迎你。否则,确认Classpath正确且没有其他应用占用8182端口。
你能在http://www.restlet.org/documentation/1.0/examples/firstSteps/sources.zip找到这个例子应用的源文件。
结尾
我们希望你喜欢这个简单的例子,我们现在鼓励你进一步阅读“第一个Resource”(http://www.restlet.org/documentation/1.0/firstResource)或开始深入Restlet Tutorial(http://www.restlet.org/documentation/1.0/tutorial)。
备注:
感谢Didier Girard提议写这篇文章。