`
ajax
  • 浏览: 251986 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

    博客分类:
  • REST
阅读更多

说明,本系列文章所使用的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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics