Restlet - 基於JAX-RS的Restlet開發實例

一、示例說明
java

   版本:Restlet版本爲2.1.0。web

   另外:這個應該是纔開始接觸級別的示例,剛學者能夠做爲借鑑看看,大神請深藏您的功與名。數據庫


二、關於Restletjson

 (1)、官網:http://restlet.org/app

 (2)、原則:爲全部「事物」即資源定義ID;將全部事物連接在一塊兒;使用標準方法,即CRUD;資源多重表述;無狀態通訊。具體描述谷歌搜索。ide


三、建立Java Web工程,添加相關Jar。文中示例工程名爲JAXRSRestletpost

231204984.png



四、建立Model,示例爲Student測試

publicclassStudent {
privateInteger id;
privateString name;
privateInteger sex;
privateInteger age;
publicStudent() {
}
/**setter/getter**/
}


五、建立BusinessObject類,示例虛擬了一個數據庫和相應的一些操做this

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類,具體看註釋url

//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 + "";
}
}


七、擴展javax.ws.rs.core.Application類

publicclassStudentApplication extendsApplication {
@Override
publicSet<Class<?>> getClasses() {
Set<Class<?>> rrcs = newHashSet<Class<?>>();
// 綁定StudentResource。有多個資源能夠在這裏綁定。
rrcs.add(StudentResource.class);
returnrrcs;
}
}


八、擴展org.restlet.ext.jaxrs.JaxRsApplication類

publiccla***estJaxRsApplication extendsJaxRsApplication {
publicRestJaxRsApplication(Context context) {
super(context);
//將StudentApplication加入了運行環境中,若是有多個Application能夠在此綁定
this.add(newStudentApplication());
}
}


九、web.xml配置

<context-param>
<param-name>org.restlet.application</param-name>
<param-value>app.RestJaxRsApplication</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>


十、Client端測試


/**
* 示例使用了Junit,不用能夠寫Main方法
*/
publicclassClient {
publicstaticString url = "http://127.0.0.1:8080/JAXRSRestlet/";
@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)、testGetXml():<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

     <Student><age>18</age><id>1</id><name>Michael</name><sex>1</sex></Student>

 (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"}

相關文章
相關標籤/搜索