In this lab, we create a simple RESTful service that returns a String “hello, world” when a client request the “http://[serverURI]/hello” resource with a HTTP/GET command. The lab is based on the Restlet framework.
This is the first tutorial to see how Restlet framework receive client requests and reply to them.
package firstREST; import org.restlet.data.Protocol; import org.restlet.resource.Get; import org.restlet.resource.ServerResource; import org.restlet.Server; public class HelloWorld extends ServerResource{ /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub // Create the HTTP server and listen on port 8182 new Server(Protocol.HTTP, 8182, HelloWorld.class).start(); } @Get public String present(){ return "hello, world"; } }
package client; import java.io.IOException; import org.restlet.resource.ClientResource; import org.restlet.resource.ResourceException; public class ClientCall { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // Create the client resource ClientResource resource = new ClientResource("http://localhost:8182"); // Write the response entity on the console try { resource.get().write(System.out); } catch (ResourceException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }