restlet2.1 学习笔记(二) 分别处理Get Post Put请求

本文详细介绍了RESTful API的基本概念及其资源类实现方式,包括如何通过注解实现不同HTTP方法的响应,并通过HTML客户端和Restlet客户端进行访问验证。同时,展示了如何利用不同HTTP请求方法(如GET、POST、PUT、DELETE)进行资源操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

servlet只支持GET与POST两种请求。

但是restlet除了支持GET与POST请求外还支持Delete  Put  OPTIONS 等多种请求 。


第一步,编写资源类

(可以将资源类想象成Struts2的Action ,每个加上注解的方法都是一个ActionMethod)

MovieResource.java

[java]  view plain copy
  1. package com.zf.restlet.demo02.server;  
  2.   
  3. import org.restlet.resource.Delete;  
  4. import org.restlet.resource.Get;  
  5. import org.restlet.resource.Post;  
  6. import org.restlet.resource.Put;  
  7. import org.restlet.resource.ServerResource;  
  8.   
  9. /** 
  10.  * 以3中Method为例 
  11.  * @author zhoufeng 
  12.  * 
  13.  */  
  14. public class MovieResource extends ServerResource{  
  15.       
  16.       
  17.     @Get  
  18.     public String play(){  
  19.         return "电影正在播放...";  
  20.     }  
  21.       
  22.       
  23.     @Post  
  24.     public String pause(){  
  25.         return "电影暂停...";  
  26.     }  
  27.       
  28.     @Put  
  29.     public String upload(){  
  30.         return "电影正在上传...";  
  31.     }  
  32.       
  33.     @Delete  
  34.     public String deleteMovie(){  
  35.         return "删除电影...";  
  36.     }  
  37.       
  38.       
  39. }  

第二步,使用html客户端访问(html默认只支持get与post访问。所以下面演示着两种)

demo02.html

[html]  view plain copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <title>demo02</title>  
  6. </head>  
  7. <body>  
  8.       
  9.       
  10.     <form action="http://localhost:8888/" method="get" target="_blank">  
  11.         <input type="submit" value="Get请求" />     
  12.     </form>  
  13.       
  14.     <form action="http://localhost:8888/" method="post" target="_blank">  
  15.         <input type="submit" value="Post请求" />     
  16.     </form>      
  17.       
  18. </body>  
  19. </html>  

访问该html通过两个按钮可以发送不同的请求,并会有不同的返回值



第三步:使用Restlet编写客户端调用

MovieClient.java

[java]  view plain copy
  1. package com.zf.restlet.demo02.client;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.junit.Test;  
  6. import org.restlet.representation.Representation;  
  7. import org.restlet.resource.ClientResource;  
  8.   
  9. public class MovieClient {  
  10.   
  11.     @Test  
  12.     public void test01() throws IOException{  
  13.         ClientResource client = new ClientResource("http://localhost:8888/");  
  14.         Representation result =  client.get() ;     //调用get方法  
  15.         System.out.println(result.getText());    
  16.     }  
  17.       
  18.     @Test  
  19.     public void test02() throws IOException{  
  20.         ClientResource client = new ClientResource("http://localhost:8888/");    
  21.         Representation result =  client.post(null) ;        //调用post方法  
  22.         System.out.println(result.getText());    
  23.     }  
  24.       
  25.       
  26.     @Test  
  27.     public void test03() throws IOException{  
  28.         ClientResource client = new ClientResource("http://localhost:8888/");    
  29.         Representation result =  client.put(null) ;     //调用put方法  
  30.         System.out.println(result.getText());    
  31.     }  
  32.       
  33.       
  34.     @Test  
  35.     public void test04() throws IOException{  
  36.         ClientResource client = new ClientResource("http://localhost:8888/");    
  37.         Representation result =  client.delete() ;      //调用delete方法  
  38.         System.out.println(result.getText());    
  39.     }  
  40.       
  41.       
  42. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值