restlet2.1 学习笔记(二) 分别处理Get Post Put请求
get
pos
處理
學習
筆記
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
<meta charset="UTF-8"> <title>demo02</title>
登入後複製
访问该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()); } }
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)