In fact, the previous tutorial shows us how restlet framework reply a client request. It is not a RESTful function since it does not attach the request to any resource and actually, any URI will work. Let's check with http://localhost:8182/hellowork, http://localhost:8182/hello/work and so on.
Now, let's attach a specific resource for a specific request.
package secondREST; import org.restlet.Component; import org.restlet.data.Protocol; public class RESTDistributor { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub // Create a new Restlet component and add a HTTP server connector to it Component component = new Component(); // if you have an HTTP connector listening on the same port (i.e. 8182), you will get an error // in this case change the port number (e.g. 8183) component.getServers().add(Protocol.HTTP, 8182); // Then attach it to the local host component.getDefaultHost().attach("/trace", RESTResource.class); // Now, let's start the component! // Note that the HTTP server connector is also automatically started. component.start(); } }
package secondREST; import org.restlet.resource.Get; import org.restlet.resource.ServerResource; public class RESTResource extends ServerResource{ @Get public String present() { // Print the requested URI path return "Resource URI : " + getReference() + '\n' + "Root URI : " + getRootRef() + '\n' + "Routed part : " + getReference().getBaseRef() + '\n' + "Remaining part: " + getReference().getRemainingPart(); } }