restlet2.1 学习笔记(二) 分别处理Get Post Put请求
2015-05-16 15:08
501 查看
servlet只支持GET与POST两种请求。
但是restlet除了支持GET与POST请求外还支持Delete Put OPTIONS 等多种请求 。
第一步,编写资源类
(可以将资源类想象成Struts2的Action ,每个加上注解的方法都是一个ActionMethod)
MovieResource.java
[java] view
plaincopy
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{
@Get
public String play(){
return "电影正在播放...";
}
@Post
public String pause(){
return "电影暂停...";
}
@Put
public String upload(){
return "电影正在上传...";
}
@Delete
public String deleteMovie(){
return "删除电影...";
}
}
第二步,使用html客户端访问(html默认只支持get与post访问。所以下面演示着两种)
demo02.html
[html] view
plaincopy
<!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
[java] view
plaincopy
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 {
@Test
public void test01() throws IOException{
ClientResource client = new ClientResource("http://localhost:8888/");
Representation result = client.get() ; //调用get方法
System.out.println(result.getText());
}
@Test
public void test02() throws IOException{
ClientResource client = new ClientResource("http://localhost:8888/");
Representation result = client.post(null) ; //调用post方法
System.out.println(result.getText());
}
@Test
public void test03() throws IOException{
ClientResource client = new ClientResource("http://localhost:8888/");
Representation result = client.put(null) ; //调用put方法
System.out.println(result.getText());
}
@Test
public void test04() throws IOException{
ClientResource client = new ClientResource("http://localhost:8888/");
Representation result = client.delete() ; //调用delete方法
System.out.println(result.getText());
}
}
但是restlet除了支持GET与POST请求外还支持Delete Put OPTIONS 等多种请求 。
第一步,编写资源类
(可以将资源类想象成Struts2的Action ,每个加上注解的方法都是一个ActionMethod)
MovieResource.java
[java] view
plaincopy
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{
@Get
public String play(){
return "电影正在播放...";
}
@Post
public String pause(){
return "电影暂停...";
}
@Put
public String upload(){
return "电影正在上传...";
}
@Delete
public String deleteMovie(){
return "删除电影...";
}
}
第二步,使用html客户端访问(html默认只支持get与post访问。所以下面演示着两种)
demo02.html
[html] view
plaincopy
<!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
[java] view
plaincopy
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 {
@Test
public void test01() throws IOException{
ClientResource client = new ClientResource("http://localhost:8888/");
Representation result = client.get() ; //调用get方法
System.out.println(result.getText());
}
@Test
public void test02() throws IOException{
ClientResource client = new ClientResource("http://localhost:8888/");
Representation result = client.post(null) ; //调用post方法
System.out.println(result.getText());
}
@Test
public void test03() throws IOException{
ClientResource client = new ClientResource("http://localhost:8888/");
Representation result = client.put(null) ; //调用put方法
System.out.println(result.getText());
}
@Test
public void test04() throws IOException{
ClientResource client = new ClientResource("http://localhost:8888/");
Representation result = client.delete() ; //调用delete方法
System.out.println(result.getText());
}
}
相关文章推荐
- android开发学习笔记(一)分别通过GET和POST请求从服务器端获取数据
- restlet2.1 学习笔记(二) 分别处理Get Post Put请求
- ASIHTTPRequest实现对PUT,DELETE,POST,GET请求方式的处理
- SpringMVC: 处理PUT和DELETE请求时报错:HTTP Status 405 - JSPs only permit GET POST or HEAD.
- 学习笔记之--post/get/put
- 学习笔记之安卓GET与POST网络请求的三种方式
- 学习笔记:get与post的区别,请求转发与请求重定向的区别
- restlet2.1 学习笔记(三) 获取简单请求参数
- HTTP POST GET PUT请求处理
- HTTP 笔记与总结(1 )Telnet 分别发送 HTTP GET 和 HTTP POST 请求
- python学习笔记:使用requests库发送get与post请求
- AJAX学习笔记之 客户端请求方式 get 和post 模式
- 学习笔记-post和get请求的区别
- 学习笔记--post 相比get 有很多优点,为什么现在的HTTP通信中大多数请求还是使用get?
- 黑马程序员之ASP.NET学习笔记: Http方法:Get请求与Post请求的区别
- Java学习笔记———《http的get请求和post请求及封装的工具类》
- javaservlet处理四种常用api请求get,put,post,delete
- CoAP学习笔记——服务器端繁忙时的处理请求流程
- 使用httpClient处理get请求或post请求
- HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误