Attaching a client request to a resource!

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.

  1. Create another Java package named secondREST and a new class inside this package named RESTDistributor. Then, just copy & paste the following codes:

    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();  
    	}	 
     
    }
  2. Have a look on the codes, we understand that the server listens to client requests on port 8182 and it will deliver all the request “/trace” to another class for execution and reply.
  3. So, now create the the Java class named “RESTResource” to execute the request. Then, add these codes:

    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();  
    	 }  
    }
  4. Well, everything is done. Let's run the RESTDistributor class by “Alt+Shift+X,J” and navigate the URIhttp://localhost:8182/trace/resource/res?para=meter” by your web browser.
  5. That's it. Also, you should verify the RESTDistributor with other URIs that does not contain the “/trace” in the URL such as: “http://localhost:8182/resource/” to see what is different from the previous example. You can also see what appears in the console whenever you request a resource.
teaching_assistant/web_services/restful_attach_resource.txt · Last modified: 2021/01/20 09:07 by IT Courses (NNC)
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0