问题
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?
回答1:
You may want to consider looking at http://www.restlet.org/documentation/ the documentation provided by the project provides good examples to get started with using the code.
Version 2.1 is currently the stable branch and the @Get, @Post, etc. annotations, available to be used on your ServerResource, provide a slightly more flexible approach than outlined by Divyesh, although that approach is I believe still also available.
回答2:
here simple code which call amazon.java rest class when its match with url as http://anydomain.com/amazone if you hit this in url than its called get method
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;
}
}
amazon.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();
}
}
and mapping in web.xml file as
<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>
来源:https://stackoverflow.com/questions/14890143/restlet-api-example