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

php中文网
发布: 2016-06-07 15:31:02
原创
1768人浏览过

servlet只支持get与post两种请求。 但是restlet除了支持GET与POST请求外还支持Delete Put OPTIONS 等多种请求 。 第一步,编写资源类 (可以将资源类想象成Struts2的Action ,每个加上注解的方法都是一个ActionMethod) MovieResource.java package com.zf.r

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{
	
	
	@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

AutoGLM沉思
AutoGLM沉思

智谱AI推出的具备深度研究和自主执行能力的AI智能体

AutoGLM沉思 239
查看详情 AutoGLM沉思
<!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 {

	@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());  
	}
	
	
}
登录后复制



最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号