RESTLET 开发实例(二) 使用Component、Application 的 REST 服务 本篇文章我们介绍不基于 JAX-RS 的模式。JAX-RS 其实就是一个简单的 Application 服务。和我们接下来介绍的 Application 基本一致,唯一不同的地方就是,不使用JAX-RS 自动映射为 xml。 一、基于 ServerResource 的 REST,来实现 JAX-RS 中 get 方法。 1、新建 RestApplication Web 工程。 然后把相应的 restlet 的 lib 下的全部 jar 加入工程引用中,然后在web.xml,加入如下配置:
org.restlet.application org.lifeba.ws.app.RestSimpleApplication RestletServlet org.restlet.ext.servlet.ServerServlet RestletServlet /* 上面的配置和基于JAX-RS 的配置一样的。 2、新建 RestSimpleApplication 对象。将应用程序和资源类绑定在一起,代码如下: public class RestSimpleApplication extends org.restlet.Application{ @Override public Restlet createInboundRoot() { Router router = new Router(getContext()); router.attach(“/student/{studentId}”, StudentResource.class); return router; } } 和JAX-RS 不同主要有 2 个地方: 1)RestSimpleApplication 直接扩展了 Application 对象,而不是 JAX-RS中的JaxRsApplication 对象。 2)重载了 createInboundRoot 通过 attach 方法绑定资源类,并且制定了访问路径。而 JAX-RS 中调用了 this.add(new StudentApplication())来绑定资源类,并且不用指定访问路径,因为是在资源类中指定。 3、新建 Student 对象,代码如下:和 JAX-RS 的区别就是少了@XmlRootElement(name=”Student”)标注。 public class Student { private int id; private String name; private int sex; private int clsId; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.nam...