Restlet combat (5) Create methods corresponding to PUT, POST, DELETE

In the previous series of articles, in order to test some function points, only the case of get is selected. This article will add three other main methods: POST, PUT, DELETE. The corresponding business methods are add, modify, and delete. This article modifies the sample code of the previous article.

 

First add code to CustomerResource:

 

Java code   Favorite code
  1. @Override  
  2. public  boolean  allowPut() {  
  3.     return true;  
  4. }     
  5.   
  6. @Override  
  7. public  boolean  allowPost() {  
  8.     return true;  
  9. }  
  10.   
  11. @Override   
  12. public boolean allowDelete() {  
  13.     return true;  
  14. }  
  15.   
  16. @Override  
  17. public void storeRepresentation(Representation entity) throws ResourceException {  
  18.     Form form = new Form(entity);  
  19.     Customer customer = new Customer();  
  20.     customer.setName(form.getFirstValue("name"));  
  21.     customer.setRemarks("This is an example which receives request with put method and save data");  
  22.       
  23.     customerDAO.saveCustomer(customer);  
  24. }  
  25.   
  26. @Override  
  27. public void acceptRepresentation(Representation entity) throws ResourceException {  
  28.     Form form = new Form(entity);  
  29.     Customer customer = new Customer();  
  30.     customer.setId(customerId);  
  31.     customer.setName(form.getFirstValue("name"));  
  32.     customer.setRemarks("This is an example which receives request with post method and update data");  
  33.       
  34.     customerDAO.updateCustomer(customer);  
  35. }  
  36.   
  37. @Override  
  38. public void removeRepresentations() throws ResourceException {  
  39.     customerDAO.deleteCustomer(customerId);  
  40. }  

 

 

这里稍微说明一下,如果你想增加put方法,则需要override方法allowPut,并使之返回值为true,同样,对post,delete是一样的,如果你觉得指定三个方法太多,那么你可以用下面的代码来替代上面三个方法:

 

Java代码   Favorite code
  1. @Override   
  2. public boolean isModifiable() {  
  3.         return true;  
  4. }  

 

在数据层的接口类和实现类里面加入相应的调用代码:

 

Java代码   Favorite code
  1. public interface CustomerDAO {  
  2.     public String getCustomerById(String id);  
  3.     public void saveCustomer(Customer customer);  
  4.     public void updateCustomer(Customer customer);  
  5.     public void deleteCustomer(String id);  
  6. }  

 

Java代码   Favorite code
  1. public class CustomerDAOImpl implements CustomerDAO{  
  2.     Logger logger = Logger.getLogger(this.getClass().getName());  
  3.       
  4.     public String getCustomerById(String id){  
  5.         //get other information through id such as name, no, address etc.  
  6.         String name = "ajax";  
  7.         String address=  "Shanghai";  
  8.         return "The customer name is " + name + " and he is from " + address;  
  9.     }  
  10.       
  11.     public void saveCustomer(Customer customer){  
  12.         //save the customer data into db  
  13.         System.out.println("save the infomation of customer into database");  
  14.     }  
  15.       
  16.     public void updateCustomer(Customer customer){  
  17.         System.out.println("update the customer whose id is " + customer.getId());  
  18.     }  
  19.       
  20.     public void deleteCustomer(String id){  
  21.         System.out.println("delete the customer whose id is " + id);  
  22.     }  
  23.       
  24. }  

 

 

为了封装传递的参数,创建一个Customer BO:

 

Java代码   Favorite code
  1. public class Customer implements Serializable{  
  2.     private static final long serialVersionUID = 4021273041291957638L;  
  3.     private String id;  
  4.     private String name;  
  5.     private String phone;  
  6.     private String address;  
  7.     private String email;  
  8.     private String remarks;  
  9.       
  10.     //getter and setter method  
  11. }  

 

下面使用Restlet提供的客户端来测试上述代码:

 

Java代码   Favorite code
  1. public class CustomerResourceTest extends TestCase{  
  2.       
  3.     public static void testStoreRepresentation(){  
  4.         Client client = new Client(Protocol.HTTP);  
  5.         Reference itemsUri = new Reference("http://localhost:8080/restlet/resources/customers/1");  
  6.         Form form = new Form();  
  7.         form.add("name""Ajax");  
  8.         form.add("description""test store presentation");  
  9.         Representation rep = form.getWebRepresentation();  
  10.         Response response = client.put(itemsUri, rep);  
  11.         assertTrue(response.getStatus().isSuccess());  
  12.     }  
  13.       
  14.     public static void testAcceptRepresentation(){  
  15.         Client client = new Client(Protocol.HTTP);  
  16.         Reference itemsUri = new Reference("http://localhost:8080/restlet/resources/customers/1");  
  17.         Form form = new Form();  
  18.         form.add("name""Ajax_cn");  
  19.         form.add("description""test update presentation");  
  20.         Representation rep = form.getWebRepresentation();  
  21.         Response response = client.post(itemsUri, rep);  
  22.         assertTrue(response.getStatus().isSuccess());  
  23.     }  
  24.       
  25.     public static void testDeleteRepresentation(){  
  26.         Client client = new Client(Protocol.HTTP);  
  27.         Reference itemsUri = new Reference("http://localhost:8080/restlet/resources/customers/1");  
  28.   
  29.         Response response = client.delete(itemsUri);  
  30.         assertTrue(response.getStatus().isSuccess());  
  31.     }  
  32. }  

 

这里唯一想说的是看测试第一个方法里面的URL的定义:

Reference itemsUri = new Reference("http://localhost:8080/restlet/resources/customers/1");

According to the division of resources, such a URL is inappropriate, and the correct URL should be:

Reference itemsUri = new Reference(" http://localhost:8080/restlet/resources/customers "); The corresponding method is PUT. If you don't understand it very well, read one of the series

Guess you like

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