Chinaunix首页 | 论坛 | 博客
  • 博客访问: 199111
  • 博文数量: 47
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1259
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-24 10:20
文章分类
文章存档

2014年(21)

2013年(26)

分类: 网络与安全

2014-01-06 11:34:52

1、示例说明

   版本:Restlet版本为2.1.0。

   相关:实例是使用Restlet自身的Application和Component组件。


2、创建Java Web工程,添加相关Jar。实例中工程名为RestletService

211714472.png

3、创建Model,示例为Student
1
2
3
4
5
6
7
8
9
    
publicclassStudent {
privateInteger id;
privateString name;
privateInteger sex;
privateInteger age;
publicStudent() {
}
/**setter/getter**/
}


4、创建BusinessObject类,示例虚拟了一个数据库和相应的一些操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    
publicclassStudentBO {
privatestaticMap students = newHashMap();
// 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 getStudentAll() {
returnnewArrayList(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;
}
}


5、创建对应的Resource类,具体看注释

   (1)、StudentResource类,主要针对单一查询,修改和删除操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    
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类,主要针对多返回查询和新增操作

1
2
3
4
5
6
7
8
9
10
11
12
    
publicclassStudentListResource extendsServerResource {
privateStudentBO studentService = newStudentBO();
@Get("json")
publicList get(Representation entity) {
List studentList = studentService.getStudentAll();
returnstudentList;
}
@Post("json")
publicInteger saveStudent(Student student) {
returnstudentService.saveOrUpdateStudent(student);
}
}


6、扩展org.restlet.Application类

1
2
3
4
5
6
7
8
9
10
11
12
    
publicclassStudentApplication extendsApplication {
/**
* 重写createInboundRoot通过attach方法绑定资源类,并且制定了访问路径
*/
@Override
publicRestlet createInboundRoot() {
Router router = newRouter(getContext());
router.attach("/student/{studentId}", StudentResource.class);
router.attach("/student", StudentListResource.class);
returnrouter;
}
}


7、配置web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
    

org.restlet.application

com.rc.rl.StudentApplication


RestletServlet
org.restlet.ext.servlet.ServerServlet


RestletServlet
/*



8、Test客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    
/**
*客户端使用了Junit4
*/
publicclassStudentClient {
@Test
publicvoidstudent_findById() {
try{
ClientResource client = newClientResource(
"");
Representation representation = client.get();
System.out.println(representation.getText());
} catch(Exception e) {
e.printStackTrace();
}
}
publicvoidstudent_delete() {
try{
ClientResource client = newClientResource(
"");
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(
"");
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(
"");
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(
"");
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(
"");
Representation representation = client.get();
System.out.println(representation.getText());
} catch(Exception e) {
e.printStackTrace();
}
}
}


说明:以上的org.restlet.Application的使用示例。


9、org.restlet.Component的使用

   在上面的实例中,如果需要加入Teacher等更多资源时,或许为了业务逻辑的分离,就不能再把TeacherResource也在StudentApplication中进行绑定。


   解决办法是如同上面所示建立Teacher相关的Resource和针对Teacher的org.restlet.Application扩展,然后扩展org.restlet.Component如下:
1
2
3
4
5
6
    
publicclassRestSimpleComponent extendsComponent {
publicRestSimpleComponent() {
getDefaultHost().attach("/stu",newStudentApplication());
getDefaultHost().attach("/tea",newTeacherApplication());
}
}


   再修改web.xml中如下:

1
2
3
4
5
6
    


org.restlet.component
com.rc.rl.RestSimpleComponent



   注意:通过如上配置之后,访问的URI需要加上Component中添加的路径,如之前的 将变更为


阅读(1300) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~