1、示例说明
版本:Restlet版本为2.1.0。
另外:这个应该是才开始接触级别的示例,刚学者可以作为借鉴看看,大神请深藏您的功与名。
2、关于Restlet
(1)、官网:http://restlet.org/
(2)、原则:为所有“事物”即资源定义ID;将所有事物链接在一起;使用标准方法,即CRUD;资源多重表述;无状态通信。具体描述谷歌搜索。
3、创建Java Web工程,添加相关Jar。文中示例工程名为JAXRSRestlet
4、创建Model,示例为Student
publicclassStudent { privateInteger id; privateString name; privateInteger sex; privateInteger age; publicStudent() { } /**setter/getter**/ }
5、创建BusinessObject类,示例虚拟了一个数据库和相应的一些操作
publicclassStudentBO { privatestaticMap<Integer, Student> students = newHashMap<Integer, Student>(); // next Id privatestaticintnextId = 5; static{ students.put(1, newStudent(1, "Michael", 1, 18)); students.put(2, newStudent(2, "Anthony", 1, 22)); students.put(3, newStudent(3, "Isabella", 0, 19)); students.put(4, newStudent(4, "Aiden", 1, 20)); } publicStudent getStudent(Integer id) { returnstudents.get(id); } publicList<Student> getStudentAll() { returnnewArrayList<Student>(students.values()); } publicInteger saveOrUpdateStudent(Student student) { if(student.getId() == null) { student.setId(nextId++); } students.put(student.getId(), student); returnstudent.getId(); } publicInteger removeStudent(Integer id) { students.remove(id); returnid; } }
6、创建对应的Resource类,具体看注释
//student路径进来的都会调用StudentResource来处理 @Path("student") publicclassStudentResource { StudentBO studentBO = newStudentBO(); // 说明了http的方法是get方法 @GET // 每个方法前都有对应path,用来申明对应uri路径 @Path("{id}/xml") // 指定返回的数据格式为xml @Produces("application/xml") // 接受传递进来的id值,其中id为Path中的{id},注意定义的占位符与@PathParam要一致 publicStudent getStudentXml(@PathParam("id") intid) { returnstudentBO.getStudent(id); } @GET @Path("{id}/json") @Produces("application/json") publicStudent getStudentJson(@PathParam("id") intid) { returnstudentBO.getStudent(id); } @POST @Path("post") publicString addStudent(Representation entity) { Form form = newForm(entity); String name = form.getFirstValue("name"); intsex = Integer.parseInt(form.getFirstValue("sex")); intage = Integer.parseInt(form.getFirstValue("age")); Student student = newStudent(); student.setName(name); student.setSex(sex); student.setAge(age); inti = studentBO.saveOrUpdateStudent(student); returni + ""; } @PUT @Path("put") publicString updateStudent(Representation entity) { Form form = newForm(entity); intid = Integer.parseInt(form.getFirstValue("id")); String name = form.getFirstValue("name"); intsex = Integer.parseInt(form.getFirstValue("sex")); intage = Integer.parseInt(form.getFirstValue("age")); Student student = newStudent(); student.setId(id); student.setName(name); student.setSex(sex); student.setAge(age); inti = studentBO.saveOrUpdateStudent(student); returni + ""; } }
7、扩展javax.ws.rs.core.Application类
publicclassStudentApplication extendsApplication { @Override publicSet<Class<?>> getClasses() { Set<Class<?>> rrcs = newHashSet<Class<?>>(); // 绑定StudentResource。有多个资源可以在这里绑定。 rrcs.add(StudentResource.class); returnrrcs; } }
8、扩展org.restlet.ext.jaxrs.JaxRsApplication类
publicclassRestJaxRsApplication extendsJaxRsApplication { publicRestJaxRsApplication(Context context) { super(context); //将StudentApplication加入了运行环境中,如果有多个Application可以在此绑定 this.add(newStudentApplication()); } }
9、web.xml配置
<context-param> <param-name>org.restlet.application</param-name> <param-value>app.RestJaxRsApplication</param-value> </context-param> <servlet> <servlet-name>RestletServlet</servlet-name> <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RestletServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
10、Client端测试
/** * 示例使用了Junit,不用可以写Main方法 */ publicclassClient { publicstaticString url = "http://127.0.0.1:8080/JAXRSRestlet/"; @Test publicvoidtestGetXml() { ClientResource client = newClientResource(url + "student/1/xml"); try{ System.out.println(client.get().getText()); } catch(ResourceException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } } @Test publicvoidtestGetJson() { ClientResource client = newClientResource(url + "student/1/json"); try{ System.out.println(client.get().getText()); } catch(ResourceException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } } @Test publicvoidtestPost() { ClientResource client = newClientResource(url + "student/post"); try{ Form form = newForm(); form.add("name", "testPost"); form.add("age", "0"); form.add("sex", "39"); String id = client.post(form.getWebRepresentation()).getText(); System.out.println(id); } catch(Exception e) { e.printStackTrace(); } } @Test publicvoidtestPut() { ClientResource client = newClientResource(url + "student/put"); try{ Form form = newForm(); form.add("id", "1"); form.add("name", "testPut"); form.add("age", "22"); form.add("sex", "0"); String id = client.put(form.getWebRepresentation()).getText(); System.out.println(id); } catch(Exception e) { e.printStackTrace(); } } @Test publicvoidtestDelete() { ClientResource client = newClientResource(url + "student/1"); try{ System.out.println(client.delete().getText()); } catch(ResourceException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } } }
1、输出结果
(1)、testGetXml():<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Student><age>18</age><id>1</id><name>Michael</name><sex>1</sex></Student>
(2)、testGetJson:{"id":1,"sex":1,"age":18,"name":"Michael"}
(3)、testPut():1
再调用testGetJson()传入{id}=1时:{"id":1,"sex":0,"age":22,"name":"testPut"}
(4)、testPost():5
再调用testGetJson()传入{id}=5时:{"id":5,"sex":39,"age":0,"name":"testPost"}