首页 > 代码库 > 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