`

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

    博客分类:
  • REST
 
阅读更多

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

 

首先在CustomerResource里加入代码:

 

Java代码  收藏代码
  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代码  收藏代码
  1. @Override   
  2. public boolean isModifiable() {  
  3.         return true;  
  4. }  

 

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

 

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

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

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

分享到:
评论

相关推荐

    restlet处理各种请求方式参考示例

    本示例将详细阐述如何使用Restlet框架处理HTTP协议中的各种请求方法,如GET、POST、PUT、DELETE等。 首先,让我们了解RESTlet的基本结构。在Restlet中,应用程序由资源(Resource)组成,这些资源是可交互的对象,...

    restlet项目

    3. **定义HTTP方法**: 在Restlet类中覆盖`get()`, `post()`, `put()`, `delete()`等方法,以处理不同类型的HTTP请求。 4. **设置路由**: 指定Restlet将响应哪些URI路径。 5. **运行应用**: 将Restlet附加到一个...

    restlet工程示例

    - 创建`ClientResource`实例,指定目标URI,然后调用其对应HTTP方法的方法,如`get()`, `post()`, `put()`, `delete()`。 5. **数据交换格式** - RESTful服务通常使用JSON或XML作为数据交换格式。Restlet提供了`...

    restlet

    REST是一种轻量级的架构风格,广泛应用于互联网应用的开发,它强调通过简单的HTTP方法(如GET、POST、PUT、DELETE)来操作资源,以实现高效、可扩展的网络通信。 RESTlet框架提供了客户端和服务器端的组件,使得...

    restlet实现最简单的restful webservice

    3. **定义路由**:在Restlet应用中,你需要创建一个路由(Route)来映射URL到对应的资源。这可以通过创建一个Application类来完成。 ```java import org.restlet.Application; import org.restlet.Restlet; ...

    基于Spring的Restlet实例

    1. **RESTful服务基础**:REST是一种软件架构风格,强调通过HTTP协议暴露资源,使用HTTP方法(GET、POST、PUT、DELETE等)来操作这些资源。理解REST的基本原则和设计模式对于创建高效的API至关重要。 2. **Spring...

    Restlet开发实例

    在这个实例中,你将学习如何使用Restlet框架结合JAX-RS来定义资源、处理HTTP方法(如GET、POST、PUT、DELETE等)以及如何处理JSON或XML数据。Restlet提供了JAX-RS兼容的组件,使得开发者可以轻松地在Restlet环境中...

    restlet入门示例

    REST架构基于HTTP协议,通过HTTP方法(GET、POST、PUT、DELETE等)来操作资源。资源通常由URI(Uniform Resource Identifier)唯一标识。在RESTful API中,每个URI对应一个资源,而HTTP方法定义了对这些资源的操作。...

    RestletClient接口调试

    REST是一种网络应用程序的设计风格和开发方式,基于HTTP协议,以资源为中心,通过统一资源标识符(URI)来定位资源,使用标准方法(GET、POST、PUT、DELETE等)进行操作。RESTful接口遵循这些原则,使得它们易于理解...

    谷歌Restlet Client插件

    它支持多种HTTP方法,如GET、POST、PUT、DELETE等,允许用户发送自定义请求头和POST数据,极大地简化了Web服务的交互过程。 在使用谷歌Restlet Client插件时,首先需要在你的谷歌浏览器上安装该插件。你可以通过...

    restlet restful

    RESTful是一种软件架构风格,设计模式,是基于HTTP协议的Web服务设计原则,强调资源的概念,通过URI来定位资源,通过HTTP方法(GET、POST、PUT、DELETE等)来操作资源。它简化了Web服务的开发,使得服务器端和客户端...

    Restlet Client

    1. **请求方法选择**:Restlet Client支持所有常见的HTTP方法,如GET、POST、PUT、DELETE、PATCH、OPTIONS等,用户可以根据需求选择相应的方法发送请求。 2. **URL构造**:在发送请求时,可以方便地输入或粘贴URL,...

    restlet-jse-2.2.1.zip

    - 处理HTTP方法:Restlet支持GET、POST、PUT、DELETE等HTTP方法,通过重写`handle`或`service`方法来定义不同方法的行为。 - 构建URI路由:使用`Router`组件,可以方便地根据请求的URI匹配不同的资源或处理逻辑。 ...

    Restlet 2.0 边学边写(八)使用jQuery和ajax实现对Restlet资源的CRUD操作

    例如,`@Post`、`@Get`、`@Put`和`@Delete`注解分别对应上述的四种操作。 至于"firstSteps"这个文件名,可能是该教程中的一个起点项目或者示例代码,它可能包含了一个简单的Restlet应用和使用jQuery进行CRUD操作的...

    org.restlet-2.3.0.jar 最新版本

    REST是一种基于HTTP协议的轻量级设计模式,强调资源的概念,通过URI(Uniform Resource Identifier)来标识资源,并通过HTTP方法(如GET、POST、PUT、DELETE等)进行操作。RESTful API的设计使得服务接口简洁且易于...

    Restlet in Action

    - 如何选择合适的HTTP方法(GET、POST、PUT、DELETE等)来设计Web服务接口。 - 实例演示:通过一个简单的示例展示如何使用Restlet框架快速搭建RESTful服务。 2. **第2章:启动Restlet应用程序**(Beginning a ...

    52-restlet.rar_restlet

    3. **路由与处理**:讲解如何使用Restlet来定义路由规则,将不同的HTTP请求映射到相应的处理函数,以及如何处理HTTP方法(GET、POST、PUT、DELETE等)。 4. **数据交换格式**:讨论Restlet支持的数据交换格式,如...

    restlet-jee-2.2.2

    1. **RESTful架构**:Restlet框架的核心理念是遵循REST原则,这包括资源定位(通过URI)、使用标准HTTP方法(GET、POST、PUT、DELETE等)操作资源、以及使用HTTP状态码来表示请求结果。 2. **组件模型**:Restlet...

Global site tag (gtag.js) - Google Analytics