Restlet combat (2) Use one Application to manage multiple resources

Note, the Restlet version used in this series of articles is 1.1.5, and the 2.0 version API has made a lot of changes. But there is no final release yet. Therefore, 2.0 will not be considered for the time being, but in the following series of articles, it will be mentioned that those functions are only available in version 2.

 

Back to the topic, since the theme is actual combat, readers may ask, why are there no specific examples and codes? Don't worry, you can't eat a fat man in one bite, take your time, or post a picture, a picture illustrating the spatial distribution of each component:


 

I would like to emphasize that this picture is still very useful. I will use the sample code combined with the source code to introduce it later.

 

The following example is based on the translated document http://www.iteye.com/topic/182843 with some changes.

 

First, we define two resources, one is Order and the other is Customer. The specific code is as follows:

 

    OrderResource.java

 

 

Java code   Favorite code
  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 code   Favorite code
  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代码   Favorite code
  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. }  

  

The configuration in web.xml is skipped, there is nothing to say, it is basically the same as the one in the above link, but the name is different

 

Start the application server (occasionally used Tomcat), assuming your context is /restlet, enter in the browser: http://localhost:8080/restlet/customers/1

 

http://localhost:8080/restlet/orders/1/2


 The browser page will display accordingly:

 

get customer id :1

 

the order id is : 1 and the sub order id is : 2

 

Seeing this, some people may ask questions. There is something wrong with your resource design. In addition, you only use one OrderApplicaiton to attach two resources. Isn't this strange?

 

Yes, yes, yes, for example, for Customers, the correct resource design should have two resources corresponding to /customers and customers/1, such as CustomersResource and CustomerResource. It should be noted that the design of resources is very important, which is mentioned in many articles, including the restful web service, and will be covered in subsequent series of articles. How to deal with the second situation. The next article will explain further.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326527461&siteId=291194637