说明,本系列文章所使用的Restlet版本是1.1.5, 2.0的版本API做了不少改动。不过现在还没有最终的release。所以暂时不会考虑2.0,不过后面的系列文章中会提到那些功能只有2的版本才有。
回到正题,既然主题是实战,可能读者会问,怎么不见具体的例子和代码?别急,一口吃不了个胖子,慢慢来,还是贴一个图,一张说明各个组件空间分布的图:
还是强调一下,这张图还是很有用的,后续会用示例代码结合源代码来介绍。
下面的例子是基于http://www.iteye.com/topic/182843这篇翻译文档的基础上做一些改变。
首先我们定义两个resource,一个是Order,一个是Customer,具体代码如下:
OrderResource.java
/**
*
* @author ajax
* @version 1.0
*/
public class OrderResource extends Resource {
String orderId = "";
String subOrderId = "";
public OrderResource(Context context, Request request,
Response response) {
super(context, request, response);
orderId = (String) request.getAttributes().get("orderId");
subOrderId = (String) request.getAttributes().get("subOrderId");
// 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(
"the order id is : " + orderId + " and the sub order id is : " + subOrderId, MediaType.TEXT_PLAIN);
return representation;
}
}
CustomerResource.java
/**
*
* @author ajax
* @version 1.0
*/
public class CustomerResource extends Resource {
String customerId = "";
public CustomerResource(Context context, Request request, Response response) {
super(context, request, response);
customerId = (String) request.getAttributes().get("custId");
// 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("get customer id :" + customerId,
MediaType.TEXT_PLAIN);
return representation;
}
}
接下来定义一个Application:
OrderApplication.java
public class OrderApplication extends Application {
/**
* Creates a root Restlet that will receive all incoming calls.
*/
@Override
public synchronized Restlet createRoot() {
Router router = new Router(getContext());
router.attach("/orders/{orderId}/{subOrderId}", OrderResource.class);
router.attach("/customers/{custId}", CustomerResource.class);
return router;
}
}
web.xml中的配置略过,没有好说的,跟上面链接里面的基本一致,只是名字不同而已
启动应用服务器(偶用的Tomcat),假定你的context是/restlet,在浏览器里面输入:http://localhost:8080/restlet/customers/1
http://localhost:8080/restlet/orders/1/2
浏览器页面就会相应的显示:
get customer id :1
the order id is : 1 and the sub order id is : 2
看到这里,可能有人会提出疑问,你的资源设计的有问题吧,另外你仅用了一个OrderApplicaiton,来attach两个资源,这个不觉的很奇怪吗?
是,是,是,比如说对Customer,正确的资源设计,应该是对应/customers和customers/1应该分别有两个资源与之对应,如CustomersResource和CustomerResource。需要说明的是,资源的设计非常重要,这个在很多文章,包括那本restful web service里面有提到,后续系列文章会涉及到。针对第二种情况,应该怎么去处理。下一篇文章会作进一步的说明。
- 大小: 28.3 KB
分享到:
相关推荐
RESTLET开发实例(二)使用Component、Application的REST服务
NULL 博文链接:https://ajaxcn.iteye.com/blog/438237
用户在安装了Restlet Client之后,直接点击插件图标即可开始使用,只需输入自己的网站并选择好对应的请求模式就能对网站发送Web请求并检查响应,同时使用者可以先将多个API请求组合成一个方案来创建一个针对网站的...
restlet相关文档
一个简单的Restlet开发框架的Basic认证的例子
restlet项目
NULL 博文链接:https://sb33060418.iteye.com/blog/1768621
Restlet与Spring 集成
Restlet项目为“建立REST概念与Java类之间的映射”提供了一个轻量级而全面的框架。它可用于实现任何种类的REST式系统,而不仅仅是REST式Web服务;而且,事实证明它自从2005年诞生之时起,就是一个可靠的软件。 ...
一个软件应可以既充当Web客户端又充当Web服务器,而无须采用两套完全不同的APIs。 编写Restlet客户端 要确保把以下JAR包写在你的classpath中,才能成功编译并运行接下来的例子: * org.restlet.jar(Restlet API)...
restlet源代码,描述了restlet框架中每个类的具体实现
restlet所需jar包一
restlet2.1学习笔记项目代码
资源里面包含了Restlet从入门到高级的开发过程,对学习Restlet有很大的帮助
Restlet Client 插件是一个类似Postman的Api接口测试工具, 界面友好, UI布局也更合理。相比Postman, Restlet Client无需安装本地客户端, 即装即用, 占用空间小, 功能强大。
RESTLET开发实例(一)基于JAX-RS的REST服务
基于Spring的Restlet实例 代码和jar包
Restlet所需要的所有jar包 一次下载,以后高枕无忧!
restlet是一个支持rest架构的JAVA编写的开源轻量级框架,比较简单易学,不错的框架。