Restlet実戦(二)1つのアプリケーションを使用して複数のリソースを管理する

3415 ワード

なお、本シリーズで使用するRestletバージョンは1.1である.5,2.0のバージョンのAPIは多くの変更をしました.しかし、まだ最終的なreleaseはありません.だからしばらく2.0は考えませんが、後のシリーズではそれらの機能は2のバージョンしかありません.
 
本題に戻って、テーマが実戦である以上、読者はどうして具体的な例とコードが見えないのかと聞くかもしれません.焦らないで、一口ではデブが食べられないので、ゆっくりして、やはり図を貼って、各コンポーネントの空間分布を説明する図です.
 
やはり強調しますが、この図は役に立ちます.後でサンプルコードとソースコードを組み合わせて紹介します.
 
次の例はhttp://www.iteye.com/topic/182843この翻訳ドキュメントに基づいていくつかの変更を行います.
 
まず、OrderとCustomerの2つのresourceを定義します.具体的なコードは次のとおりです.
 
    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;
	}
}

 
次に、アプリケーションを定義します.
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を1つしか使っていません.attachの2つのリソースを使っていますが、これは不思議ですか.
 
はい、そうです.例えば、Customerに対して、正しいリソース設計は、/customersとcustomers/1に対応する2つのリソースが対応しているはずです.例えば、CustomersResourceとCustomerResourceなどです.説明する必要があるのは、リソースの設計が非常に重要で、これは多くの文章で、そのrestful webサービスを含めて、後続のシリーズの文章に関連しています.2つ目の場合、どのように処理すればいいですか.次の文章はさらに説明します.