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

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

 

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

 

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

 

下面的例子是基于http://www.javaeye.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. }  
/**
 * 
 * @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

 

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. }  
/**
 * 
 * @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

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值