Restlet combat (11) Modify the Restlet-Spring configuration file with source code

The last article combined the source code of Restlet to analyze the configuration file of Restlet-spring, and raised related questions. This article will do a test to answer this question.

 

First modify the Spring configuration file:

 

Java code   Favorite code
  1. <bean id="restRoute" class="org.restlet.ext.spring.SpringRouter">  
  2.     <property name="attachments">  
  3.         <map>  
  4.             <entry key="/customers">  
  5.                 <bean class="org.restlet.ext.spring.SpringFinder">  
  6.                     <lookup-method name="createResource" bean="customersResource" />  
  7.                 </bean>  
  8.             </entry>  
  9.             <entry key="/customers/{customerId}">  
  10.                 <bean class="org.restlet.ext.spring.SpringFinder">  
  11.                     <lookup-method name="createResource" bean="customerResource" />  
  12.                 </bean>  
  13.             </entry>  
  14.             <entry key="/users">  
  15.                 <bean class="com.mycompany.restlet.application.UserApplication"/>  
  16.             </entry>  
  17.         </map>  
  18.     </property>  
  19. </bean>  

 

In addition to configuring customer, an Application related to User is also configured:

 

Java code   Favorite code
  1. public class UserApplication extends Application{  
  2.     @Override  
  3.     public synchronized Restlet createRoot() {  
  4.         Router router = new Router(getContext());  
  5.   
  6.         router.attach("/{userId}", UserResource.class);  
  7.         router.attach("/{userId}/orders", UserOrdersResource.class);  
  8.         return router;  
  9.     }  
  10. }  

 

In order to test more clearly, we did not modify the Customer related classes, and created two resource files: UserResource and UserOrdersResource. The corresponding URLs should be: /users/{userId} and /users/{userId}/orders. One URL means to query a certain user according to the id, and the second is to query to obtain all the orders corresponding to a certain user.

 

Java code   Favorite code
  1. public class UserResource extends Resource {  
  2.     String userId;  
  3.       
  4.     public UserResource(Context context, Request request, Response response) {  
  5.         super(context, request, response);  
  6.         userId = (String) request.getAttributes().get("userId");  
  7.         getVariants().add(new Variant(MediaType.TEXT_PLAIN));  
  8.     }  
  9.       
  10.     @Override  
  11.     public Representation represent(Variant variant) {  
  12.         String userMsg = "current user id is " + userId;  
  13.         Representation representation = new StringRepresentation(userMsg,  
  14.                 MediaType.TEXT_PLAIN);  
  15.         return representation;  
  16.     }  
  17.       
  18. }  

 

Java代码   Favorite code
  1. public class UserOrdersResource extends Resource {  
  2.     String userId;  
  3.       
  4.     public UserOrdersResource(Context context, Request request, Response response) {  
  5.         super(context, request, response);  
  6.         getVariants().add(new Variant(MediaType.TEXT_PLAIN));  
  7.         userId = (String) request.getAttributes().get("userId");  
  8.     }  
  9.       
  10.     @Override  
  11.     public Representation represent(Variant variant) {  
  12.         String userMsg = "get all orders for user whose id is: " + userId;  
  13.         Representation representation = new StringRepresentation(userMsg,  
  14.                 MediaType.TEXT_PLAIN);  
  15.         return representation;  
  16.     }  
  17. }  

 

It should be noted that the above two resources are directly attached to the Application, instead of being taken over by SpringFinder as mentioned in the previous article. Therefore, the previous article emphasized some rules, such as a no-parameter constructor, an init method, etc., just Does not apply to the definitions of the two resources above. 

 

There is no other code, let's test it, open the browser, enter http://localhsot:8080/restlet/resources/users/1 , the page will display:

 

current user id is 1

And after entering http://localhost:8080/restlet/resources/users/1/orders  , the page will display:

 

get all orders for user whose id is: 1

 

We then test what happens if value is a String:

Guess you like

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