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

2014年(21)

2013年(26)

分类: 网络与安全

2014-01-06 11:33:49

1、相关说明

   version:文中示例使用的Spring版本为3.0.3,Restlet版本为2.1.0。

   entity:Student


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

022030176.jpg

说明:动态代理的cglib-nodep.jar不可缺少。


3、web.xml配置
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
    

dispatcherServlet
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath*:/applicationContext.xml




org.springframework.web.context.ContextLoaderListener



org.springframework.web.util.IntrospectorCleanupListener


encodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8


forceEncoding
true




restlet
org.restlet.ext.spring.SpringServerServlet

org.restlet.application
application



restlet
/*



4、Model实体类代码
1
2
3
4
5
6
7
8
9
10
    
@JsonSerialize(include = Inclusion.NON_NULL)
publicclassStudent {
privateInteger id;
privateString name;
privateInteger sex;
privateInteger age;
publicStudent() {
}
/**setter/getter**/
}

说明:@JsonSerialize(include = Inclusion.NON_NULL)为JackSon的注解,作用是返回JSON格式的实体时不返回该实体类值为Null的Field,如: {"id" : 1,"name" : null,"sex" : 1,"age" : 20} 。


5、Resource代码

 (1)、StudentResource:示例中主要针对单个实例的Retrieve、Update和Delete。

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
    
@Controller
@Scope("prototype")
publicclassStudentResource extendsServerResource{
privateInteger id;
@Autowired
privateStudentBO studentBO;
publicvoidsetStudentBO(StudentBO studentBO) {
this.studentBO = studentBO;
}
@Override
protectedvoiddoInit() throwsResourceException {
id = Integer.valueOf((String) getRequestAttributes().get("studentId"));
}
@Get("json")
publicStudent findStudentById(){
Student s = this.studentBO.getStudent(id);
returns;
}
@Delete("json")
publicInteger deleteStudentById() {
returnthis.studentBO.removeStudent(id);
}
@Put("json")
publicInteger updateStudent(Student student) {
student.setId(id);
returnthis.studentBO.saveOrUpdateStudent(student);
}
}


(2)、StudentListResource:示例中主要针对单个实例的Create和多个实例的Retrieve。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    
@Controller
@Scope("prototype")
publicclassStudentListResource extendsServerResource {
@Autowired
privateStudentBO studentBO;
@Post
publicInteger saveStudent(Student student) {
returnstudentBO.saveOrUpdateStudent(student);
}
@Get("json")
publicList findStudentAll() {
returnstudentBO.getStudentAll();
}
publicvoidsetStudentBO(StudentBO studentBO) {
this.studentBO = studentBO;
}
}

说明:接受Spring管理的Bean,@Scope("prototype")的annotation必须要有,否则会出现正确调用指定方法的问题,用xml配置Bean时也必须要Attribute:scope。


6、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
29
30
    
@Service
@Scope("prototype")
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;
}
}


7、Spring applictionContext.xml配置



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    
























说明:该application在web.xml中引用


8、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
    
publicclassStudentClient {
publicvoidstudent_get(){
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();
}
}
publicvoidstudent_put(){
try{
ClientResource client = newClientResource(
"");
Student student = newStudent("Test_Put", 1, 18);
Representation representation = client.put(
student, MediaType.APPLICATION_JAVA_OBJECT);
System.out.println(representation.getText());
} catch(Exception e) {
e.printStackTrace();
}
}
publicvoidstudent_post(){
try{
ClientResource client = newClientResource(
"");
Student student = newStudent("Test_Post", 1, 18);
Representation representation = client.post(
student, MediaType.APPLICATION_JAVA_OBJECT);
System.out.println(representation.getText());
} catch(Exception e) {
e.printStackTrace();
}
}
publicvoidstudent_findAll(){
try{
ClientResource client = newClientResource(
"");
Representation representation = client.get();
System.out.println(representation.getText());
} catch(Exception e) {
e.printStackTrace();
}
}
publicstaticvoidmain(String[] args) {
StudentClient client =  newStudentClient();
client.student_get();
}
}

9、补充

   (1)、请求参数问题:客户端发送形如/student/{studentId}的URI请求时,可能是还需要携带其他parameter的,参数携带形式可如/student/{studentId}?name=John&sex=23;Server端获取参数时,针对在URI{}内的参数使用getRequest().getAttributes().get("studentId");方式获取,针对附加参数可以通过Form form = getRequest().getResourceRef().getQueryAsForm(); String name = form.getValues("name");方式获取。

   (2)、客户端工具:测试Server端时,如果不想写Client端code,可以是WizTools.org的RestClient-ui.jar的小工具,Post和Put请求时的对象可以以JSON格式添加到Http body中即可。

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