restlet处理各种请求方式参考示例
1.新建web工程,项目结构如下:
1.编写实体类Student.java:
package test;public class Student {private String name;private String sex;private int age;public Student(String name,String sex,int age){this.name = name;this.sex = sex;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
3.编写资源处理类FirstServerResource.java:
package test;import org.restlet.data.Form;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;//定义一个资源
public class FirstServerResource extends ServerResource {private int id;//用来获取传递过来的id占位符的值@Overrideprotected void doInit() throws ResourceException{id = Integer.valueOf((String)getRequestAttributes().get("id"));}//处理get请求@Overridepublic Representation get(){StringRepresentation str = new StringRepresentation("hello world " + id);return str;}//处理post请求protected Representation post(Representation entity) { //获取表单值:Form form = new Form(entity); String name = form.getFirstValue("name"); String sex = form.getFirstValue("sex"); //将客户端提交的表单值返回return new StringRepresentation(id + "所对应的name:" + name + ",sex:" + sex) ; }//处理put请求protected Representation put(Representation entity) { //获取表单值:Form form = new Form(entity); String name = form.getFirstValue("name"); String sex = form.getFirstValue("sex"); //将客户端提交的表单值返回return new StringRepresentation(id + "所对应的name:" + name + ",sex:" + sex) ; }//处理delete请求public Representation delete(){//模拟删除//...........return new StringRepresentation(id + "所对应的资源已删除");}}
4.编写服务应用类ServerApplication.java:
package test;import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;//为资源配置路径
public class ServerApplication extends Application {@Overridepublic Restlet createInboundRoot() {Router router = new Router(this.getContext());// 绑定资源路径"hello"到对应的处理资源类(FirstServerResource)// 接收传入的参数"id"router.attach("/hello/{id}", FirstServerResource.class);return router;}}
5.编写组件类MyComponent.java:
package test;import org.restlet.Component;public class MyComponent extends Component {/*** 重写createInboundRoot通过attach方法绑定资源类,并且制定了访问路径* */public MyComponent() {getDefaultHost().attach("/test", new ServerApplication());}
}
6.编写服务启动类Server.java:
package test;import org.restlet.Component;
import org.restlet.data.Protocol;public class Server {public static void main(String[] args) throws Exception {Component component = new MyComponent(); component.getServers().add(Protocol.HTTP, 8182); component.start(); }
}
7.编写客户端测试类TestClient.java:
package test;import java.io.IOException;import org.junit.Test;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;public class TestClient {/** 测试get请求方式 */@Testpublic void testGet() throws IOException {String url = "http://localhost:8182/test/hello/1001";ClientResource client = new ClientResource(url);Representation representation = client.get();// getSystem.out.println("*******get测试结果********");System.out.println(representation.getText());}/** 测试post请求方式 */@Testpublic void testPost() throws IOException {Form form = new Form();form.add("name", "zhuxun");form.add("sex", "M");String url = "http://localhost:8182/test/hello/1001";ClientResource client = new ClientResource(url);// 以post方式提交表单Representation representation = client.post(form); // postSystem.out.println("*******post测试结果********");System.out.println(representation.getText());}/** 测试put请求方式 */@Testpublic void testPut() throws IOException {Form form = new Form();form.add("name", "zhuxun");form.add("sex", "M");String url = "http://localhost:8182/test/hello/1001";ClientResource client = new ClientResource(url);// 以post方式提交表单Representation representation = client.put(form,MediaType.APPLICATION_JAVA_OBJECT); // putSystem.out.println("*******put测试结果********");System.out.println(representation.getText());}/** 测试delete请求方式 */@Testpublic void testDelete() throws IOException {String url = "http://localhost:8182/test/hello/1001";ClientResource client = new ClientResource(url);Representation representation = client.delete();// deleteSystem.out.println("*******delete测试结果********");System.out.println(representation.getText());}}
7.分别启动服务类Server.java和测试类TestClient.java
可以看到如下测试结果:
注:上面的测试用到了Junit测试工具
如果要下载本项目,可以访问:http://download.csdn.net/detail/u012875880/6607299