欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Restlet实战(五)创建对应PUT、POST、DELETE的方法

程序员文章站 2022-03-06 12:31:39
...

之前的系列文章,为了测试一些功能点,所以只选择get这种情况,本文将添加另外三种主要的方法POST, PUT, DELETE.对应我们的业务方法是新增、修改、删除。此文对上篇文章示例代码进行修改。

 

首先在CustomerResource里加入代码:

 

@Override
public boolean allowPut() {
	return true;
}	

@Override
public boolean allowPost() {
	return true;
}

@Override 
public boolean allowDelete() {
	return true;
}

@Override
public void storeRepresentation(Representation entity) throws ResourceException {
	Form form = new Form(entity);
	Customer customer = new Customer();
	customer.setName(form.getFirstValue("name"));
	customer.setRemarks("This is an example which receives request with put method and save data");
	
	customerDAO.saveCustomer(customer);
}

@Override
public void acceptRepresentation(Representation entity) throws ResourceException {
	Form form = new Form(entity);
	Customer customer = new Customer();
	customer.setId(customerId);
	customer.setName(form.getFirstValue("name"));
	customer.setRemarks("This is an example which receives request with post method and update data");
	
	customerDAO.updateCustomer(customer);
}

@Override
public void removeRepresentations() throws ResourceException {
	customerDAO.deleteCustomer(customerId);
}

 

 

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

 

@Override 
public boolean isModifiable() {
        return true;
}

 

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

 

public interface CustomerDAO {
	public String getCustomerById(String id);
	public void saveCustomer(Customer customer);
	public void updateCustomer(Customer customer);
	public void deleteCustomer(String id);
}

 

public class CustomerDAOImpl implements CustomerDAO{
	Logger logger = Logger.getLogger(this.getClass().getName());
	
	public String getCustomerById(String id){
		//get other information through id such as name, no, address etc.
		String name = "ajax";
		String address=  "Shanghai";
		return "The customer name is " + name + " and he is from " + address;
	}
	
	public void saveCustomer(Customer customer){
		//save the customer data into db
		System.out.println("save the infomation of customer into database");
	}
	
	public void updateCustomer(Customer customer){
		System.out.println("update the customer whose id is " + customer.getId());
	}
	
	public void deleteCustomer(String id){
		System.out.println("delete the customer whose id is " + id);
	}
	
}

 

 

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

 

public class Customer implements Serializable{
	private static final long serialVersionUID = 4021273041291957638L;
	private String id;
	private String name;
	private String phone;
	private String address;
	private String email;
	private String remarks;
	
	//getter and setter method
}

 

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

 

public class CustomerResourceTest extends TestCase{
	
	public static void testStoreRepresentation(){
		Client client = new Client(Protocol.HTTP);
		Reference itemsUri = new Reference("http://localhost:8080/restlet/resources/customers/1");
		Form form = new Form();
		form.add("name", "Ajax");
		form.add("description", "test store presentation");
		Representation rep = form.getWebRepresentation();
		Response response = client.put(itemsUri, rep);
		assertTrue(response.getStatus().isSuccess());
	}
	
	public static void testAcceptRepresentation(){
		Client client = new Client(Protocol.HTTP);
		Reference itemsUri = new Reference("http://localhost:8080/restlet/resources/customers/1");
		Form form = new Form();
		form.add("name", "Ajax_cn");
		form.add("description", "test update presentation");
		Representation rep = form.getWebRepresentation();
		Response response = client.post(itemsUri, rep);
		assertTrue(response.getStatus().isSuccess());
	}
	
	public static void testDeleteRepresentation(){
		Client client = new Client(Protocol.HTTP);
		Reference itemsUri = new Reference("http://localhost:8080/restlet/resources/customers/1");

		Response response = client.delete(itemsUri);
		assertTrue(response.getStatus().isSuccess());
	}
}

 

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

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

按照资源的划分,这样的URL是不合适的,正确的URL应该是:

Reference itemsUri = new Reference("http://localhost:8080/restlet/resources/customers");相应的method是PUT。如果不是很理解,看这个系列中的一篇

相关标签: Ajax