这些天在看一本叫做《REST实战》的书,觉得内容有点难明白的,好像十分的抽象的。虽然有了HTTP的基础,但是,还是觉得很抽象,模糊得很。
决定做一个实验,体验一下REST实实在在摸得到的感觉。开始寻找第一感觉,记录第一次的感性认识。
如下步骤进行:
1.寻找指导
冲浪去寻找答案,如下网址有助于入门:
(1).http://www.restlet.org/documentation/1.0/tutorial
(2). http://wiki.restlet.org/docs_2.0/13-restlet/21-restlet/378-restlet.html
2.下载restlet包
寻找一个工具包,如下两个地方有很多,上一个新一点(2.0),下一个旧一点(1.0)。
(1)http://www.restlet.org/downloads/
(2)http://webscripts.softpedia.com/script/Development-Scripts-js/Restlet-45528.html
3.布置环境(myeclipse+jdk6.0)
对下载的工具包进行解压,包很多,还分了很多个文件夹来的。在文件夹里面的包也有用的。如果不用会报错。
打开myeclipse,创建一个web project。引入工具包中的所的包,包括里面的子文件夹的包。
4.用官网的代码来测试
import org.restlet.Component; import org.restlet.Restlet; import org.restlet.data.MediaType; import org.restlet.data.Protocol; import org.restlet.data.Request; import org.restlet.data.Response; public class TestMain { public static void main(String[] args) throws Exception { // Create a new Restlet component and add a HTTP server connector to it Component component = new Component(); component.getServers().add(Protocol.HTTP, 8182); // Create a new tracing Restlet Restlet restlet = new Restlet() { @Override public void handle(Request request, Response response) { // Print the requested URI path String message = "Resource URI : " + request.getResourceRef() + '\n' + "Root URI : " + request.getRootRef() + '\n' + "Routed part : " + request.getResourceRef().getBaseRef() + '\n' + "Remaining part: " + request.getResourceRef().getRemainingPart(); response.setEntity(message, MediaType.TEXT_PLAIN); } }; // Then attach it to the local host component.getDefaultHost().attach("/trace", restlet); // Now, let's start the component! // Note that the HTTP server connector is also automatically started. component.start(); } }
5.执行
在浏览器输入如下的东西,会有如下的显示
6.用servlet来试一试
(1)创建一个资源resource
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 TestResource extends Resource { public TestResource(Context context, Request request, Response response) { super(context, request, response); getVariants().add(new Variant(MediaType.TEXT_PLAIN)); } @Override public Representation getRepresentation(Variant variant) { Representation representation = new StringRepresentation( "Success, Great!", MediaType.TEXT_PLAIN); return representation; } }
(2)一个应用类application
import org.restlet.Application; import org.restlet.Context; import org.restlet.Restlet; import org.restlet.Router; public class TestApplication extends Application { public TestApplication(Context pc) { super(pc); } @Override public synchronized Restlet createRoot() { Router r = new Router(getContext()); r.attachDefault(TestResource.class); return r; } }
(3)配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>org.restlet.application</param-name> <param-value>TestApplication</param-value> </context-param> <servlet> <servlet-name>RestletServlet</servlet-name> <servlet-class>com.noelios.restlet.ext.servlet.ServerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RestletServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
(4)浏览器查看结果情况