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()); } }