Restlet hello 例子
作者:互联网
Restlet 是开源的用于Java 开发REST服务框架。
官方网址:https://restlet.talend.com/
以下是官方文档中例子
1 创建项目
引用: compile 'org.restlet.jse:org.restlet:2.4.1'
2 测试程序
创建资源
import org.restlet.resource.Get; import org.restlet.resource.ServerResource; public class HelloWorldResource extends ServerResource { @Get public String represent() { return "hello, world"; } }
定义资源路由
import org.restlet.Application; import org.restlet.Restlet; import org.restlet.routing.Router; public class FirstStepsApplication extends Application { @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; } }
使用内置web容器发布rest服务
import org.restlet.Component; import org.restlet.data.Protocol; public class AppTest { public static void main(String[] args) { // 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. try { component.start(); } catch (Exception e) { e.printStackTrace(); } } }
运行AppTest 在浏览器测试访问:http://localhost:8182/firstSteps/hello,出现已下说明成功。
标签:restlet,Restlet,component,new,public,例子,import,org,hello 来源: https://blog.51cto.com/14602923/2465189