0

Is there any simple example of Restlet API with Java?

I want a simple example of Restlet API by calling Get / POST method. One client should call one method from the server through Restlet. The server should execute that method and send the reply accordingly. How can the server open those methods to respond to the client using Restlet?

4

2 回答 2

1

这里是简单的代码,当它与 url 匹配时调用 amazon.java 休息类作为 http://anydomain.com/amazone如果你在 url 中点击它而不是它调用的 get 方法

public class RestApi extends Application {

/**
 * Creates a root Restlet that will receive all incoming calls.
 */
@Override
public Restlet createInboundRoot() {
    Router router = new Router(getContext());

    // Defines only one route
    router.attach("/amazon", Amazon.class);
    return router;
}
}

亚马逊.java

public class Amazon extends ServerResource {

@Override
protected Representation post(Representation entity)
        throws ResourceException {
    System.out.println("post Method");
    return super.post(entity);

}

@Override
protected Representation get() throws ResourceException {
    System.out.println("get method");
    return super.get();
}

}

并将 web.xml 文件中的映射为

 <servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
<init-param>
    <param-name>org.restlet.application</param-name>
    <param-value>com.wa.gwtamazon.server.RestApi </param-value>
</init-param>

<!-- Catch all requests -->
<servlet-mapping>
    <servlet-name>RestletServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
于 2013-02-15T11:04:03.233 回答
0

您可能需要考虑查看http://www.restlet.org/documentation/项目提供的文档提供了很好的示例来开始使用代码。

2.1 版目前是稳定的分支,可用于您的 ServerResource 的 @Get、@Post 等注释提供了比 Divyesh 概述的稍微灵活的方法,尽管我相信该方法仍然可用。

于 2013-02-15T12:20:50.790 回答