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

2014年(21)

2013年(26)

分类: 网络与安全

2014-01-06 11:34:19

1、示例说明

   版本:Restlet版本为2.1.0。

   另外:这个应该是才开始接触级别的示例,刚学者可以作为借鉴看看,大神请深藏您的功与名。


2、关于Restlet

 (1)、官网:

 (2)、原则:为所有“事物”即资源定义ID;将所有事物链接在一起;使用标准方法,即CRUD;资源多重表述;无状态通信。具体描述谷歌搜索。


3、创建Java Web工程,添加相关Jar。文中示例工程名为JAXRSRestlet

231204984.png



4、创建Model,示例为Student

1
2
3
4
5
6
7
8
9
    
publicclassStudent {
privateInteger id;
privateString name;
privateInteger sex;
privateInteger age;
publicStudent() {
}
/**setter/getter**/
}


5、创建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;
}
}


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

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
    
//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类

1
2
3
4
5
6
7
8
9
    
publicclassStudentApplication extendsApplication {
@Override
publicSet> getClasses() {
Set> rrcs = newHashSet>();
// 绑定StudentResource。有多个资源可以在这里绑定。
rrcs.add(StudentResource.class);
returnrrcs;
}
}


8、扩展org.restlet.ext.jaxrs.JaxRsApplication类

1
2
3
4
5
6
7
    
publicclassRestJaxRsApplication extendsJaxRsApplication {
publicRestJaxRsApplication(Context context) {
super(context);
//将StudentApplication加入了运行环境中,如果有多个Application可以在此绑定
this.add(newStudentApplication());
}
}


9、web.xml配置
1
2
3
4
5
6
7
8
9
10
11
12
    

org.restlet.application
app.RestJaxRsApplication


RestletServlet
org.restlet.ext.servlet.ServerServlet


RestletServlet
/*



10、Client端测试


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
    
/**
* 示例使用了Junit,不用可以写Main方法
*/
publicclassClient {
publicstaticString url = "";
@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():

     181Michael1

 (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"}
阅读(1421) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~