Restlet实战(六)-正确设计资源

在上篇文章的末尾,我提到资源的设计有一点问题,增加客户的功能应该放到Customers resource而不是Customer Resource。本文首先会改正这个问题。

 

首先把创建Customer的代码从Customer Resource移到Customers Resource,另外创建一个查询所有customer的get方法,代码如下:

 

Java代码   收藏代码
  1. public class CustomersResource extends Resource {  
  2.     private CustomerDAO customerDAO;  
  3.       
  4.     @Override  
  5.     public void init(Context context, Request request, Response response) {  
  6.         super.init(context, request, response);  
  7.     }  
  8.       
  9.     public CustomersResource(){  
  10.         getVariants().add(new Variant(MediaType.TEXT_PLAIN));   
  11.     }  
  12.       
  13.     public CustomersResource(Context context, Request request, Response response) {  
  14.         super(context, request, response);  
  15.           
  16.         getVariants().add(new Variant(MediaType.TEXT_PLAIN));  
  17.     }     
  18.       
  19.   
  20.     @Override  
  21.     public Representation represent(Variant variant) {  
  22.         List<Customer> list = customerDAO.getAllCustomers();  
  23.         Representation representation = new StringRepresentation("", MediaType.TEXT_PLAIN);  
  24.         return representation;  
  25.     }  
  26.       
  27.     @Override  
  28.     public boolean allowPut() {  
  29.         return true;  
  30.     }  
  31.       
  32.     @Override  
  33.     public void storeRepresentation(Representation entity) throws ResourceException {  
  34.         Form form = new Form(entity);  
  35.         Customer customer = new Customer();  
  36.         customer.setName(form.getFirstValue("name"));  
  37.         customer.setAddress(form.getFirstValue("address"));  
  38.         customer.setRemarks("This is an example which receives request with put method and save data");  
  39.           
  40.         customerDAO.saveCustomer(customer);  
  41.     }  
  42.   
  43.     public void setCustomerDAO(CustomerDAO customerDAO) {  
  44.         this.customerDAO = customerDAO;  
  45.     }  
  46.       
  47. }  

 

 在Spring配置文件定义一个Customers resource bean,并修改URL映射部分:

Xml代码   收藏代码
  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.         </map>  
  15.     </property>  
  16. </bean>  

 

Xml代码   收藏代码
  1. <bean id="customersResource" class="com.infosys.restlet.resource.CustomersResource" scope="prototype">  
  2.     <property name="customerDAO" ref="customerDAO" />  
  3. </bean>  

 

 在CustomerResourceTest中修改测试方法:

Java代码   收藏代码
  1. public static void testStoreRepresentation(){  
  2.     Client client = new Client(Protocol.HTTP);  
  3.     Reference itemsUri = new Reference("http://localhost:8080/restlet/resources/customers");  
  4.     Form form = new Form();  
  5.     form.add("name""Ajax");  
  6.     form.add("description""test store presentation");  
  7.     Representation rep = form.getWebRepresentation();  
  8.     Response response = client.put(itemsUri, rep);  
  9.     assertTrue(response.getStatus().isSuccess());  
  10. }  

 

猜你喜欢

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