Restlet实战(二)使用一个Application管理多个资源

说明,本系列文章所使用的Restlet版本是1.1.5, 2.0的版本API做了不少改动。不过现在还没有最终的release。所以暂时不会考虑2.0,不过后面的系列文章中会提到那些功能只有2的版本才有。

 

回到正题,既然主题是实战,可能读者会问,怎么不见具体的例子和代码?别急,一口吃不了个胖子,慢慢来,还是贴一个图,一张说明各个组件空间分布的图:


 

还是强调一下,这张图还是很有用的,后续会用示例代码结合源代码来介绍。

 

下面的例子是基于http://www.iteye.com/topic/182843这篇翻译文档的基础上做一些改变。

 

首先我们定义两个resource,一个是Order,一个是Customer,具体代码如下:

 

    OrderResource.java

 

 

Java代码   收藏代码
  1. /** 
  2.  *  
  3.  * @author ajax 
  4.  * @version 1.0 
  5.  */  
  6. public class OrderResource extends Resource {  
  7.     String orderId = "";  
  8.     String subOrderId = "";  
  9.       
  10.     public OrderResource(Context context, Request request,     
  11.             Response response) {     
  12.         super(context, request, response);    
  13.         orderId = (String) request.getAttributes().get("orderId");  
  14.         subOrderId = (String) request.getAttributes().get("subOrderId");  
  15.         // This representation has only one type of representation.     
  16.         getVariants().add(new Variant(MediaType.TEXT_PLAIN));     
  17.     }     
  18.       
  19.     @Override    
  20.     public Representation getRepresentation(Variant variant) {     
  21.         Representation representation = new StringRepresentation(     
  22.                 "the order id is : " + orderId + " and the sub order id is : " + subOrderId, MediaType.TEXT_PLAIN);     
  23.         return representation;     
  24.     }  
  25. }  

 

 CustomerResource.java

 

Java代码   收藏代码
  1. /** 
  2.  *  
  3.  * @author ajax 
  4.  * @version 1.0 
  5.  */  
  6. public class CustomerResource extends Resource {  
  7.     String customerId = "";  
  8.       
  9.     public CustomerResource(Context context, Request request, Response response) {  
  10.         super(context, request, response);  
  11.         customerId = (String) request.getAttributes().get("custId");  
  12.         // This representation has only one type of representation.  
  13.         getVariants().add(new Variant(MediaType.TEXT_PLAIN));  
  14.     }  
  15.   
  16.     @Override  
  17.     public Representation getRepresentation(Variant variant) {  
  18.         Representation representation = new StringRepresentation("get customer id :" + customerId,  
  19.                 MediaType.TEXT_PLAIN);  
  20.         return representation;  
  21.     }  
  22. }  

 

接下来定义一个Application:

OrderApplication.java

 

Java代码   收藏代码
  1. public class OrderApplication extends Application {  
  2.     /** 
  3.      * Creates a root Restlet that will receive all incoming calls. 
  4.      */  
  5.     @Override  
  6.     public synchronized Restlet createRoot() {  
  7.         Router router = new Router(getContext());  
  8.   
  9.         router.attach("/orders/{orderId}/{subOrderId}", OrderResource.class);  
  10.         router.attach("/customers/{custId}", CustomerResource.class);  
  11.         return router;  
  12.     }  
  13.   
  14. }  

  

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里面有提到,后续系列文章会涉及到。针对第二种情况,应该怎么去处理。下一篇文章会作进一步的说明。

猜你喜欢

转载自chenjianfei2016.iteye.com/blog/2355180