restlet2.1学习笔记(二)分别处理GetPostPut请求
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了restlet2.1学习笔记(二)分别处理GetPostPut请求,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2330字,纯文字阅读大概需要4分钟。
内容图文
![restlet2.1学习笔记(二)分别处理GetPostPut请求](/upload/InfoBanner/zyjiaocheng/533/4ddf063d6f9348f5b51b092888fc39af.jpg)
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
demo02
访问该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()); } }
内容总结
以上是互联网集市为您收集整理的restlet2.1学习笔记(二)分别处理GetPostPut请求全部内容,希望文章能够帮你解决restlet2.1学习笔记(二)分别处理GetPostPut请求所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。