一、示例說明
java
版本:Restlet版本爲2.1.0。web
相關:實例是使用Restlet自身的Application和Component組件。數據庫
二、建立Java Web工程,添加相關Jar。實例中工程名爲RestletServicejson
三、建立Model,示例爲Studentapp
publicclassStudent { privateInteger id; privateString name; privateInteger sex; privateInteger age; publicStudent() { } /**setter/getter**/ }
四、建立BusinessObject類,示例虛擬了一個數據庫和相應的一些操做ide
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; } }
五、建立對應的Resource類,具體看註釋post
(1)、StudentResource類,主要針對單一查詢,修改和刪除操做url
publicclassStudentResource extendsServerResource { privateintid; privateStudentBO studentService = newStudentBO(); /** * 用來獲取傳遞過來的studentId佔位符的值 */ @Override protectedvoiddoInit() throwsResourceException { id = Integer.valueOf((String) getRequestAttributes().get("studentId")); } @Get("json") publicStudent getStudent(){ returnstudentService.getStudent(id); } @Delete publicInteger deleteStudent() { returnstudentService.removeStudent(id); } @Put("json") publicInteger updateStudent(Student student){ student.setId(id); returnstudentService.saveOrUpdateStudent(student); } /* * 第二種傳入參數和返回值的方式 * @Put * public Representation put(Representation entity) throws ResourceException { * //entity這樣一個對象將會把客戶端傳進來參數保存在其中,經過以下方式能夠獲取參數值 * Form form = new Form(entity); * Student student = new Student(); * String name = form.getFirstValue("name"); * int sex = Integer.parseInt(form.getFirstValue("sex")); * int age = Integer.parseInt(form.getFirstValue("age")); * student.setName(name); * student.setSex(sex); * student.setAge(age); * student.setId(id); * studentService.saveOrUpdateStudent(student); * //實例返回的是String類型的擴展,固然你也能夠返回JsonRepresentation這樣一個擴展 * return new StringRepresentation(student.toString()); //爲了更好的說明返回整個對象 * * } */ }
(2)、StudentListResource類,主要針對多返回查詢和新增操做spa
publicclassStudentListResource extendsServerResource { privateStudentBO studentService = newStudentBO(); @Get("json") publicList<Student> get(Representation entity) { List<Student> studentList = studentService.getStudentAll(); returnstudentList; } @Post("json") publicInteger saveStudent(Student student) { returnstudentService.saveOrUpdateStudent(student); } }
六、擴展org.restlet.Application類rest
publicclassStudentApplication extendsApplication { /** * 重寫createInboundRoot經過attach方法綁定資源類,而且制定了訪問路徑 */ @Override publicRestlet createInboundRoot() { Router router = newRouter(getContext()); router.attach("/student/{studentId}", StudentResource.class); router.attach("/student", StudentListResource.class); returnrouter; } }
七、配置web.xml
<context-param> <param-name>org.restlet.application</param-name> <!-- 自定義org.restlet.Application擴展類 --> <param-value>com.rc.rl.StudentApplication</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>
八、Test客戶端
/** *客戶端使用了Junit4 */ publicclassStudentClient { @Test publicvoidstudent_findById() { try{ ClientResource client = newClientResource( "http://localhost:8080/RestletService/student/1"); Representation representation = client.get(); System.out.println(representation.getText()); } catch(Exception e) { e.printStackTrace(); } } publicvoidstudent_delete() { try{ ClientResource client = newClientResource( "http://localhost:8080/RestletService/student/1"); Representation representation = client.delete(); System.out.println(representation.getText()); } catch(Exception e) { e.printStackTrace(); } } @Test publicvoidstudent_put() { try{ Student student = newStudent("Test_Put", 0, 23); ClientResource client = newClientResource( "http://localhost:8080/RestletService/student/2"); Representation representation = client.put(student, MediaType.APPLICATION_JAVA_OBJECT); System.out.println(representation.getText()); } catch(Exception e) { e.printStackTrace(); } } /** * StudentResource中第二種傳入參數和返回值的方式的客戶端調用方式 */ @Test publicvoidstudent_put_other() { try{ Form queryForm = newForm(); queryForm.add("name", "steven4"); queryForm.add("sex", "2"); queryForm.add("age", "300"); ClientResource client = newClientResource( "http://localhost:8080/RestletService/student/2"); Representation representation = client.put(queryForm.getWebRepresentation()); System.out.println(representation.getText()); } catch(Exception e) { e.printStackTrace(); } } @Test publicvoidstudent_post() { try{ Student student = newStudent("Test_Put", 0, 23); ClientResource client = newClientResource( "http://localhost:8080/RestletService/student"); Representation representation = client.post(student, MediaType.APPLICATION_JAVA_OBJECT); System.out.println(representation.getText()); } catch(Exception e) { e.printStackTrace(); } } @Test publicvoidstudent_getAll() { try{ ClientResource client = newClientResource( "http://localhost:8080/RestletService/student"); Representation representation = client.get(); System.out.println(representation.getText()); } catch(Exception e) { e.printStackTrace(); } } }
說明:以上的org.restlet.Application的使用示例。
九、org.restlet.Component的使用
在上面的實例中,若是須要加入Teacher等更多資源時,或許爲了業務邏輯的分離,就不能再把TeacherResource也在StudentApplication中進行綁定。
解決辦法是如同上面所示創建Teacher相關的Resource和針對Teacher的org.restlet.Application擴展,而後擴展org.restlet.Component以下:
publiccla***estSimpleComponent extendsComponent { publicRestSimpleComponent() { getDefaultHost().attach("/stu",newStudentApplication()); getDefaultHost().attach("/tea",newTeacherApplication()); } }
再修改web.xml中<context-param/>以下:
<context-param> <!-- <param-name>org.restlet.application</param-name> <param-value>com.rc.rl.RestSimpleApplication</param-value> --> <param-name>org.restlet.component</param-name> <param-value>com.rc.rl.RestSimpleComponent</param-value> </context-param>
注意:經過如上配置以後,訪問的URI須要加上Component中添加的路徑,如以前的 http://localhost:8080/RestletService/student/1將變動爲 http://localhost:8080/RestletService/stu/student/1