本例子是采用JAX-RS的API开发的,这种方式提供了一种基于注解的模型来描述分布式资源,可以利用注解的功能提供资源的位置、传递等。可以在一个Resource类中同时对外提供多个rest接口服务。具体的实现步骤见如下章节。
2 实例开发
2.1 工程说明
此处可以建立一个web工程,也可以建立一个JAVASE工程,如何应用就UP TO YOU了,此处的例子只是为了梳理下RESTLET的开发流程,不涉及到web界面的应用,所以就建立了一个普通的JAVASE的工程。工程的结构图如下:
我们的工程分为如下几个部分:Server启动模块、Application模块、modle模块、REST接口实现的resource模块和客户端模块。
工程所依赖的jar包,是最基本的这个6个包,因为数据的传输使用的JSON,所以json包是必须的,如果想扩展功能,请自行添加其余的包。
2.2 Server端启动部分
RestJaxRsServer类的代码如下:
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 }
此代码中指定了我们的HTTP的绑定端口,地址就是本地默认的ip。另外,代码中有一个RestJaxRsApplication,这是每个Application能够管理一组restlet接口。
2.3 Application部分
在application包中有两个类,一个是RestJaxRsApplication类,继承了org.restlet.ext.jaxrs.JaxRsApplication,作为运行类,用于初始化REST的运行环境,其代码如下:
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 }
另外一个是MyApplication,作为应用类,继承了javax.ws.rs.core.Application,这里面绑定了我们的RESTLET的接口RESOURCE类,可以将多个资源绑定在HashSet中,代码如下:
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 }
2.4 模型部分
此实例的操作模型比较简单,就是一个Student类和一个Teacher类,代码如下:
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 Strin