restlet 感觉上是一个简单的servlet 用它必须先下载jar包,去restlet.com 下载 废话不多讲 上代码先
首先要有个资源类,这个资源类就是返回到界面看的数据 它继承了ServeRsource类
1
public class HelloWorldResource extends ServerResource{ @Get public String represent() { return "hello, world"; } } 2 配置访问地址 Application 就相当于管理路由的地址,由他设置一些你访问的地址
import org.restlet.Application; import org.restlet.Restlet; import org.restlet.routing.Router; public class FirstStepsApplication extends Application { /** * Creates a root Restlet that will receive all incoming calls. */ @Override public synchronized Restlet createInboundRoot() { // Create a router Restlet that routes each call to a new instance of HelloWorldResource. Router router = new Router(getContext()); // Defines only one route router.attach("/hello", HelloWorldResource.class); return router; } } 3 米和锅准备好了后就差把火了,有这把火就能基本的运行起来这个程序了 Component组件就是设置一些传输协议,端口和第一层地址,这是这3个程序的访问第一的路口
import org.restlet.Component; import org.restlet.data.Protocol; public class MovieClient { public static void main(String[] args) throws Exception { // Create a new Component. Component component = new Component(); // Add a new HTTP server listening on port 8182. component.getServers().add(Protocol.HTTP, 8182); // Attach the sample application. component.getDefaultHost().attach("/firstSteps", new FirstStepsApplication()); // Start the component. component.start(); } }
运行3后,只要在浏览器里面输入localhost:8182/firstStep/hello 就能显示出资源类中的String hello,world 拉。