RESTLET development examples

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The 1 premise

  Because of the need to work in the near future, to the application of RESTLET to the project, and the reference on the Internet based on practice data, a small example of a RESTLET interface.

  The idea of Restlet is: between HTTP client and HTTP server on the structure difference, it doesn't matter. A software should be both a Web client and a Web server, without using two different sets of APIs.

  Restlet provides multiple versions: Java SE, Java EE, Android, Google AppEngine, Google Web Toolkit, Android. Here we are using the Jee version.

  RESTLET can be implemented using JAX-RS, can also be used in other ways, see:

This example is the use of JAX-RS development of the API, this approach provides a description of distributed resource annotation based models, can use the comment function providing the location of resources, transfer etc.. In a Resource class and provide multiple rest interface service. Specific implementation steps see the following section.

2 examples of the development

2.1 project description

Here you can create a web project, you can also set up a JAVASE project, how to use the UP TO YOU, this example is to comb the development process of RESTLET, is not related to the application of web interface, so we established a common JAVASE project. Structure engineering are as follows:


Our project is divided into the following parts: Server module, Application module, modle module, resource module and client module REST interface implementation.

The project relies on the jar package, is the most basic of the 6 packages, because data transmission using JSON, so the JSON package is a must, if you want to extend the function, please add the rest of the pack.

2.2 Server end start part

The code for the RestJaxRsServer class.:

 1 package com.scott.restlet;
 2 
 3 import org.restlet.Component;
 4 import org.restlet.data.Protocol;
 5 
 6 import com.scott.restlet.application.RestJaxRsApplication;
 7 
 8 public class RestJaxRsServer {
 9     
10     public static void main(String[] args) throws Exception {
11         Component component = new Component();
12         component.getServers().add(Protocol.HTTP, 8082);
13         component.getDefaultHost().attach(new RestJaxRsApplication(null));
14         component.start();
15         
16         System.out.println("The restlet server started ...");
17     }
18 }

Bind port our HTTP specified in this code, the address is the local default ip. In addition, there is a RestJaxRsApplication code, each of which is able to manage a group of restlet interface Application.

The 2.3 part Application

In the application package has two kinds, one is the RestJaxRsApplication class, a successor to the org.restlet.ext.jaxrs.JaxRsApplication, as a class, used to run the environment initialization of REST, the code is as follows:

 1 package com.scott.restlet.application;
 2 
 3 import org.restlet.Context;
 4 import org.restlet.ext.jaxrs.JaxRsApplication;
 5 
 6 
 7 public class RestJaxRsApplication extends JaxRsApplication {
 8 
 9     public RestJaxRsApplication(Context context) {
10         super(context);
11         this.add(new MyApplication());
12     }
13 
14 }

Another is MyApplication, as an application, a successor to the javax.ws.rs.core.Application, which binds the interface class RESOURCE our RESTLET, you can connect multiple resource binding in HashSet, code is as follows:

 1 package com.scott.restlet.application;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 import javax.ws.rs.core.Application;
 7 
 8 import com.scott.restlet.resource.StudentResource;
 9 import com.scott.restlet.resource.TeacherResource;
10 
11 public class MyApplication extends Application {
12 
13     @Override
14     public Set<Class<?>> getClasses() {
15         Set<Class<?>> resources = new HashSet<Class<?>>();
16         
17         resources.add(StudentResource.class);
18         resources.add(TeacherResource.class);
19         
20         return resources;
21     }
22 
23 }

The 2.4 part model

The operation model of this example is relatively simple, is a class Student and a Teacher class, the code as follows:

Student:

 1 package com.scott.restlet.modle;
 2 
 3 public class Student {
 4 
 5     private int id;
 6     private String name;
 7     private int sex;
 8     private int age;
 9     private int grade;
10 
11     public int getGrade() {
12         return grade;
13     }
14 
15     public void setGrade(int grade) {
16         this.grade = grade;
17     }
18 
19     public int getId() {
20         return id;
21     }
22 
23     public void setId(int id) {
24         this.id = id;
25     }
26 
27     public String getName() {
28         return name;
29     }
30 
31     public void setName(String name) {
32         this.name = name;
33     }
34 
35     public int getSex() {
36         return sex;
37     }
38 
39     public void setSex(int sex) {
40         this.sex = sex;
41     }
42 
43     public int getAge() {
44         return age;
45     }
46 
47     public void setAge(int age) {
48         this.age = age;
49     }
50 }

Teacher:

 1 package com.scott.restlet.modle;
 2 
 3 public class Teacher {
 4     
 5     private int id;    
 6     private String name;
 7     private int sex;
 8     private int age;
 9     private String subject;
10     
11     public int getId() {
12         return id;
13     }
14     public void setId(int id) {
15         this.id = id;
16     }
17     public String getName() {
18         return name;
19     }
20     public void setName(String name) {
21         this.name = name;
22     }
23     public int getSex() {
24         return sex;
25     }
26     public void setSex(int sex) {
27         this.sex = sex;
28     }
29     public int getAge() {
30         return age;
31     }
32     public void setAge(int age) {
33         this.age = age;
34     }
35     public String getSubject() {
36         return subject;
37     }
38     public void setSubject(String subject) {
39         this.subject = subject;
40     }
41 }

The 2.5 part RESOURCE

This part is to realize the RESTLET interface we, here we have realized two interfaces, one is StudentResource, one is TeacherResource, which provides REST service interface of different. As for the StorageOperator class, see it as a main memory database is good, is actually a HashMap, StudentResource and TeacherResource operations implemented in the StorageOperator class. The following code:

StudentResource:

 1 package com.scott.restlet.resource;
 2 
 3 import javax.ws.rs.DELETE;
 4 import javax.ws.rs.GET;
 5 import javax.ws.rs.POST;
 6 import javax.ws.rs.PUT;
 7 import javax.ws.rs.Path;
 8 import javax.ws.rs.PathParam;
 9 import javax.ws.rs.Produces;
10 
11 import org.restlet.data.Form;
12 import org.restlet.representation.Representation;
13 
14 import com.scott.restlet.modle.Student;
15 
16 @Path("/TestRestlet/student/")
17 public class StudentResource {
18 
19     @GET
20     @Path("{id}/json")
21     @Produces("application/json")
22     public Student getStudentJson(@PathParam("id") int id) {
23         return StorageOperator.findStudent(id);
24     }
25 
26     @POST
27     @Path("add")
28     public String addStudent(Representation entity) {
29         
30         //get parameters from client
31         Form form = new Form(entity);
32         String name = form.getFirstValue("name");
33         int grade = Integer.parseInt(form.getFirstValue("grade"));
34         int sex = Integer.parseInt(form.getFirstValue("sex"));
35         int age = Integer.parseInt(form.getFirstValue("age"));
36         
37         Student student = new Student();
38         student.setGrade(grade);
39         student.setName(name);
40         student.setSex(sex);
41         student.setAge(age);
42         
43         int id = StorageOperator.studentID + 1;
44         student.setId(id);
45         return String.valueOf(StorageOperator.addStudent(student));
46     }
47 
48     @PUT
49     @Path("update")
50     public String updateStudent(Representation entity) {
51         Form form = new Form(entity);
52 
53         int id = Integer.parseInt(form.getFirstValue("id"));
54         Student student = StorageOperator.findStudent(id);
55 
56         if (student == null) {
57             return "null";
58         }else{
59             String name = form.getFirstValue("name");
60             int grade = Integer.parseInt(form.getFirstValue("grade"));
61             int sex = Integer.parseInt(form.getFirstValue("sex"));
62             int age = Integer.parseInt(form.getFirstValue("age"));
63 
64             student.setGrade(grade);
65             student.setName(name);
66             student.setSex(sex);
67             student.setAge(age);
68 
69             return String.valueOf(StorageOperator.updateStudent(student));
70         }
71     }
72 
73     @DELETE
74     @Path("delete/{id}")
75     public String deleteStudent(@PathParam("id") int id) {
76         int status = StorageOperator.deleteStudent(id);
77         return String.valueOf(status);
78     }
79 
80 }

TeacherResource:

 1 package com.scott.restlet.resource;
 2 
 3 import javax.ws.rs.DELETE;
 4 import javax.ws.rs.GET;
 5 import javax.ws.rs.POST;
 6 import javax.ws.rs.PUT;
 7 import javax.ws.rs.Path;
 8 import javax.ws.rs.PathParam;
 9 import javax.ws.rs.Produces;
10 
11 import org.restlet.data.Form;
12 import org.restlet.representation.Representation;
13 
14 import com.scott.restlet.modle.Teacher;
15 
16 @Path("/TestRestlet/teacher/")
17 public class TeacherResource {
18     @GET
19     @Path("{id}/json")
20     @Produces("application/json")
21     public Teacher getTeacherJson(@PathParam("id") int id) {
22         return StorageOperator.findTeacher(id);
23     }
24 
25     @POST
26     @Path("add")
27     public String addTeacher(Representation entity) {
28         
29         //get parameters from client
30         Form form = new Form(entity);
31         String name = form.getFirstValue("name");
32         String subject = form.getFirstValue("subject");
33         int sex = Integer.parseInt(form.getFirstValue("sex"));
34         int age = Integer.parseInt(form.getFirstValue("age"));
35         
36         Teacher teacher = new Teacher();
37         teacher.setSubject(subject);
38         teacher.setName(name);
39         teacher.setSex(sex);
40         teacher.setAge(age);
41         
42         int id = StorageOperator.teacherID + 1;
43         teacher.setId(id);
44         return String.valueOf(StorageOperator.addTeacher(teacher));
45     }
46 
47     @PUT
48     @Path("update")
49     public String updateTeacher(Representation entity) {
50         Form form = new Form(entity);
51 
52         int id = Integer.parseInt(form.getFirstValue("id"));
53         Teacher teacher = StorageOperator.findTeacher(id);
54 
55         if (teacher == null) {
56             return "null";
57         }else{
58             String name = form.getFirstValue("name");
59             String subject = form.getFirstValue("subject");
60             int sex = Integer.parseInt(form.getFirstValue("sex"));
61             int age = Integer.parseInt(form.getFirstValue("age"));
62 
63             teacher.setSubject(subject);
64             teacher.setName(name);
65             teacher.setSex(sex);
66             teacher.setAge(age);
67 
68             return String.valueOf(StorageOperator.updateTeacher(teacher));
69         }
70     }
71 
72     @DELETE
73     @Path("delete/{id}")
74     public String deleteTeacher(@PathParam("id") int id) {
75         int status = StorageOperator.deleteTeacher(id);
76         return String.valueOf(status);
77     }
78 }

StorageOperator:

 1 package com.scott.restlet.resource;
 2 
 3 import java.util.HashMap;
 4 
 5 import com.scott.restlet.modle.Student;
 6 import com.scott.restlet.modle.Teacher;
 7 
 8 public class StorageOperator {
 9     public static int studentID = 1;
10     public static int teacherID = 1;
11     
12     public static HashMap<Integer, Student> students = new HashMap<Integer, Student>();
13     public static HashMap<Integer, Teacher> teachers = new HashMap<Integer, Teacher>();
14     
15     static {
16         Student student = new Student();
17         student.setId(1);
18         student.setGrade(3);
19         student.setName("Scott");
20         student.setSex(0);
21         student.setAge(18);
22         students.put(student.getId(), student);
23         
24         Teacher teacher = new Teacher();
25         teacher.setId(1);
26         teacher.setSubject("MATH");
27         teacher.setName("Toney");
28         teacher.setSex(1);
29         teacher.setAge(27);
30         teachers.put(teacher.getId(), teacher);
31     }
32 
33     public static Student findStudent(int id) {
34         return students.get(id);
35     }
36 
37     public static int addStudent(Student student) {
38         students.put(student.getId(), student);
39         return student.getId();
40     }
41 
42     public static int updateStudent(Student student) {
43         return addStudent(student);
44     }
45 
46     public static int deleteStudent(int id) {
47         if (students.get(id) != null) {
48             students.remove(id);
49             return 1;
50         }
51         return 0;
52     }
53     
54     public static Teacher findTeacher(int id) {
55         return teachers.get(id);
56     }
57 
58     public static int addTeacher(Teacher teacher) {
59         teachers.put(teacher.getId(), teacher);
60         return teacher.getId();
61     }
62 
63     public static int updateTeacher(Teacher teacher) {
64         return addTeacher(teacher);
65     }
66 
67     public static int deleteTeacher(int id) {
68         if (teachers.get(id) != null) {
69             teachers.remove(id);
70             return 1;
71         }
72         return 0;
73     }
74 
75 }

Marked in the class above PATH annotations, a path variables as global, method of upper PATH annotations in the class notes as a relative path. And, the returned data format Produce, here is of type JSON. As for GET and POST, can be simply understood as:

   POST   /uri

Establish 

   DELETE /uri/XXX delete

   PUT

/uri/XXX update or create  

   GET

/uri/XXX view 

The 2.6 part of the client

To access the REST interface client, can enter the URL access through the browser, can also through the code to access, code here package accessed by code in the Client class, the code as follows:

StudentOperateClient:

 1 package com.scott.restlet.client;
 2 
 3 import java.io.IOException;
 4 
 5 import org.restlet.data.Form;
 6 import org.restlet.resource.ClientResource;
 7 import org.restlet.resource.ResourceException;
 8 
 9 public class StudentOperateClient {
10     public static void testGetStudent(String url) {
11         ClientResource client = new ClientResource(url + "student/1/json");
12         try {
13             System.out.println("Student get " + client.get().getText());
14         } catch (ResourceException e) {
15             e.printStackTrace();
16         } catch (IOException e) {
17             e.printStackTrace();
18         }
19     }
20 
21     public static void testAddStudent(String url) {
22         ClientResource client = new ClientResource(url + "student/add");
23         try {
24             Form form = new Form();
25             form.add("name", "Scott007");
26             form.add("grade", "3");
27             form.add("sex", "0");
28             form.add("age", "15");
29             
30             String id = client.post(form.getWebRepresentation()).getText();
31             
32             System.out.println("Student add " + id);
33         } catch (Exception e) {
34             e.printStackTrace();
35         }
36     }
37 
38     public static void testUpdateStudent(String url) {
39         ClientResource client = new ClientResource(url + "student/update");
40         try {
41             Form form = new Form();
42             form.add("name", "Scott007");
43             form.add("grade", "4");
44             form.add("sex", "0");
45             form.add("id", "1");
46             form.add("age", "16");
47             
48             String id = client.put(form.getWebRepresentation()).getText();
49             
50             System.out.println("Student update " + id);
51         } catch (Exception e) {
52             e.printStackTrace();
53         }
54     }
55 
56     public static void testDeleteStudent(String url) {
57         ClientResource client = new ClientResource(url + "student/delete/1");
58         try {
59             System.out.println("Student delete " + client.delete().getText());
60         } catch (ResourceException e) {
61             e.printStackTrace();
62         } catch (IOException e) {
63             e.printStackTrace();
64         }
65     }
66 }

TeacherOperateClient:

 1 package com.scott.restlet.client;
 2 
 3 import java.io.IOException;
 4 
 5 import org.restlet.data.Form;
 6 import org.restlet.resource.ClientResource;
 7 import org.restlet.resource.ResourceException;
 8 
 9 public class TeacherOperateClient {
10     public static void testGetTeacher(String url) {
11         ClientResource client = new ClientResource(url + "teacher/1/json");
12         try {
13             System.out.println("Teacher get " + client.get().getText());
14         } catch (ResourceException e) {
15             e.printStackTrace();
16         } catch (IOException e) {
17             e.printStackTrace();
18         }
19     }
20 
21     public static void testAddTeacher(String url) {
22         ClientResource client = new ClientResource(url + "teacher/add");
23         try {
24             Form form = new Form();
25             
26             form.add("name", "Scott008");
27             form.add("subject", "MATH");
28             form.add("sex", "0");
29             form.add("age", "27");
30             
31             String id = client.post(form.getWebRepresentation()).getText();
32             
33             System.out.println("Teacher add " + id);
34         } catch (Exception e) {
35             e.printStackTrace();
36         }
37     }
38 
39     public static void testUpdateTeacher(String url) {
40         ClientResource client = new ClientResource(url + "teacher/update");
41         try {
42             Form form = new Form();
43             
44             form.add("age", "28");
45             form.add("name", "Scott008");
46             form.add("subject", "English");
47             form.add("sex", "0");
48             form.add("id", "1");
49             
50             String id = client.put(form.getWebRepresentation()).getText();
51             System.out.println("Teacher update " + id);
52 
53         } catch (Exception e) {
54             e.printStackTrace();
55         }
56     }
57 
58     public static void testDeleteTeacher(String url) {
59         ClientResource client = new ClientResource(url + "teacher/delete/1");
60         try {
61             System.out.println("Teacher delete " + client.delete().getText());
62         } catch (ResourceException e) {
63             e.printStackTrace();
64         } catch (IOException e) {
65             e.printStackTrace();
66         }
67     }
68 }

Client:

 1 package com.scott.restlet.client;
 2 
 3 public class Client {
 4 
 5     public static final String url = "http://localhost:8082/TestRestlet/";
 6 
 7     public static void main(String args[]){
 8         
 9         StudentOperateClient.testGetStudent(url);
10         StudentOperateClient.testUpdateStudent(url);
11         
12         TeacherOperateClient.testGetTeacher(url);
13         TeacherOperateClient.testUpdateTeacher(url);
14     }
15 }

2.7 operation

Operation, the results are as follows:

The server starts:

2013-9-3 23:22:51 org.restlet.engine.http.connector.HttpServerHelper start
Information: Starting the internal HTTP server on port 8082
The restlet server started ...

After the start of the client information client:

2013-9-3 23:24:12 org.restlet.engine.http.connector.HttpClientHelper start
Information: Starting the default HTTP client
Student get {"id":1,"sex":0,"age":18,"name":"Scott","grade":3}
2013-9-3 23:24:12 org.restlet.engine.http.connector.HttpClientHelper start
Information: Starting the default HTTP client
Student update 1
2013-9-3 23:24:12 org.restlet.engine.http.connector.HttpClientHelper start
Information: Starting the default HTTP client
Teacher get {"id":1,"sex":1,"subject":"MATH","age":27,"name":"Toney"}
2013-9-3 23:24:12 org.restlet.engine.http.connector.HttpClientHelper start
Information: Starting the default HTTP client
Teacher update 1

The client starts the server information:

2013-9-3 23:22:51 org.restlet.engine.http.connector.HttpServerHelper start
Information: Starting the internal HTTP server on port 8082
The restlet server started ...
2013-9-3 23:24:12 org.restlet.engine.log.LogFilter afterHandle
Information: 2013-09-03 23:24:12 127.0.0.1 - 8082 GET    /TestRestlet/student/1/json    -    200    -    0    70    http://localhost:8082    Restlet-Framework/2.0.6    -
2013-9-3 23:24:12 org.restlet.engine.log.LogFilter afterHandle
Information: 2013-09-03 23:24:12 127.0.0.1 - 8082 PUT    /TestRestlet/student/update    -    200    1    39    4    http://localhost:8082    Restlet-Framework/2.0.6    -
2013-9-3 23:24:12 org.restlet.engine.log.LogFilter afterHandle
Information: 2013-09-03 23:24:12 127.0.0.1 - 8082 GET    /TestRestlet/teacher/1/json    -    200    -    0    1    http://localhost:8082    Restlet-Framework/2.0.6    -
2013-9-3 23:24:13 org.restlet.engine.log.LogFilter afterHandle
Information: 2013-09-03 23:24:13 127.0.0.1 - 8082 PUT    /TestRestlet/teacher/update    -    200    1    47    2    http://localhost:8082    Restlet-Framework/2.0.6    -

Of course, the scale and function examples here are very small, is in reference to the [2] on the basis of the slightly modified, can be considered as the first step in learning RESTLET.

3 some reference.

[1]

[2]

[3]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Cindy at November 14, 2013 - 8:32 AM