restlet2.1 学习笔记(二) 分别处理Get Post Put请求
来源:互联网 发布:白石洲新塘网络 编辑:程序博客网 时间:2024/06/08 12:22
servlet只支持GET与POST两种请求。
但是restlet除了支持GET与POST请求外还支持Delete Put OPTIONS 等多种请求 。
第一步,编写资源类
(可以将资源类想象成Struts2的Action ,每个加上注解的方法都是一个ActionMethod)
MovieResource.java
package com.zf.restlet.demo02.server;import org.restlet.resource.Delete;import org.restlet.resource.Get;import org.restlet.resource.Post;import org.restlet.resource.Put;import org.restlet.resource.ServerResource;/** * 以3中Method为例 * @author zhoufeng * */public class MovieResource extends ServerResource{@Getpublic String play(){return "电影正在播放...";}@Postpublic String pause(){return "电影暂停...";}@Putpublic String upload(){return "电影正在上传...";}@Deletepublic String deleteMovie(){return "删除电影...";}}
第二步,使用html客户端访问(html默认只支持get与post访问。所以下面演示着两种)
demo02.html
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>demo02</title></head><body><form action="http://localhost:8888/" method="get" target="_blank"><input type="submit" value="Get请求" /> </form><form action="http://localhost:8888/" method="post" target="_blank"><input type="submit" value="Post请求" /> </form> </body></html>
访问该html通过两个按钮可以发送不同的请求,并会有不同的返回值
第三步:使用Restlet编写客户端调用
MovieClient.java
package com.zf.restlet.demo02.client;import java.io.IOException;import org.junit.Test;import org.restlet.representation.Representation;import org.restlet.resource.ClientResource;public class MovieClient {@Testpublic void test01() throws IOException{ClientResource client = new ClientResource("http://localhost:8888/");Representation result = client.get() ;//调用get方法System.out.println(result.getText()); }@Testpublic void test02() throws IOException{ClientResource client = new ClientResource("http://localhost:8888/"); Representation result = client.post(null) ;//调用post方法System.out.println(result.getText()); }@Testpublic void test03() throws IOException{ClientResource client = new ClientResource("http://localhost:8888/"); Representation result = client.put(null) ;//调用put方法System.out.println(result.getText()); }@Testpublic void test04() throws IOException{ClientResource client = new ClientResource("http://localhost:8888/"); Representation result = client.delete() ;//调用delete方法System.out.println(result.getText()); }}
- restlet2.1 学习笔记(二) 分别处理Get Post Put请求
- restlet2.1 学习笔记(二) 分别处理Get Post Put请求
- HTTP POST GET PUT请求处理
- restlet2.1 学习笔记(三) 获取简单请求参数
- 学习笔记之--post/get/put
- restlet2.1 学习笔记(一) 入门
- restlet2.1 学习笔记(一) 入门
- HttpClient (POST GET PUT)请求
- post get delete put 请求
- android开发学习笔记(一)分别通过GET和POST请求从服务器端获取数据
- nodejs学习笔记(五)——处理get和post请求
- Http之Get,Post,put,delete请求
- java中怎样用post、get、put请求
- okhttp,get,post,put,delete 请求
- python发送get/post/put/delete请求
- get,post,del,put请求封装
- 浏览器请求方法get、post、put
- get,post,delete,put的http请求。
- android项目之连接数据库(eclipse(java)+soap协议+webservice服务
- hdoj1072
- linux内存管理解析----linux物理,线性内存布局及页表的初始化
- Dalvik虚拟机的运行过程分析
- JS动态添加网址参数,修改网址参数值的自定义功能函数
- restlet2.1 学习笔记(二) 分别处理Get Post Put请求
- 在传智学的是“方法”
- python os 模块
- 深入浅出Flex Viewer (二)——体系结构
- AVR Studio 中使用 sprintf 和 获取浮点支持
- 【Android游戏开发之一】设置全屏以及绘画简单的图形
- linux chmod命令参数及用法详解--文件文件夹权限设定命令
- expdp评估导出文件大小
- iScroll4 滚动到底部检测