首页 > 代码库 > restlet入门搭建hello world
restlet入门搭建hello world
最近研究rest接口,发现restlet应用挺广泛,不过入门资料很少,去官网下载jar包,根据官网说明搭建了一个简单的restlet工程:
1、首先导入jar包
- org.restlet.jar
- org.restlet.ext.servlet.jar
2、编写业务逻辑类
package com.chen.rest; import org.restlet.resource.Get; import org.restlet.resource.ServerResource; /** * @author chenjie * @version 1.0 * @since 2017-02-08 */ public class HelloWorld extends ServerResource { @Get public String represent() { return "hello world!"; } }
3、编写应用类
package com.chen.rest; import org.restlet.Application; import org.restlet.Restlet; import org.restlet.routing.Router; /** * @author chenjie * @version 1.0 * @since 2017-02-08 */ public class FirstApp extends Application { @Override public synchronized Restlet createInboundRoot() { System.out.println("1111111111"); Router router = new Router(getContext()); router.attach("/hello",HelloWorld.class); return router; } }
4、配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>first steps servlet</display-name> <!-- Restlet adapter --> <servlet> <servlet-name>RestletServlet</servlet-name> <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class> <init-param> <!-- Application class name --> <param-name>org.restlet.application</param-name> <param-value>com.chen.rest.FirstApp</param-value> </init-param> </servlet> <!-- Catch all requests --> <servlet-mapping> <servlet-name>RestletServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
5、部署工程到tomcat,启动tomcat,访问
http://localhost:8080/myRest/hello
restlet入门搭建hello world
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。