Restlet实战(十一)结合源代码修改Restlet-Spring配置文件

上篇文章结合了Restlet的源码分析了Restlet-spring的配置文件,并提出了相关的问题,本篇将对这一问题做一个测试解答。

 

首先修改一下Spring的配置文件:

 

Java代码   收藏代码
  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>  

 

除了配置customer外,还配置了一个跟User相关的Application:

 

Java代码   收藏代码
  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. }  

 

为了测试更加的清晰,不在Customer相关类上面做修改了,另外建立两个资源文件:UserResource和UserOrdersResource,对应的URL应该分别是:/users/{userId}和/users/{userId}/orders,第一个URL意思是根据id查询得到某个user,而第二个则是查询得到某个user对应的所有的order。

 

Java代码   收藏代码
  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代码   收藏代码
  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. }  

 

需要注意的是,上面两个资源是直接attach到Application,而不是之前文章讲的那样由SpringFinder接管,所以,之前文章强调一些规则,如需要有一个无参的构造函数,一个init方法等,就不适用于上述的两个资源的定义。 

 

没有别的代码,来测试一下吧,打开浏览器,输入http://localhsot:8080/restlet/resources/users/1后,页面会显示:

 

current user id is 1

而输入http://localhost:8080/restlet/resources/users/1/orders 后,页面会显示:

 

get all orders for user whose id is: 1

 

我们接着测试,如果value是String,会怎么样:

猜你喜欢

转载自chenjianfei2016.iteye.com/blog/2355190