Restlet Hello World

Restlet is a lightweight, comprehensive, open source REST framework for the Java platform. Restlet is suitable for both server and client Web applications. Restlet directly models the concepts (Resource, Representation, Connector, Component, etc.) from Roy T. Fielding’s original dissertation on the architecture of the Web.

Here is a Hello World example of Restlet. In our example we write a class HelloServerResource that extends a Restlet framework class “ServerResource” and contains a method named ‘greet’ decorated with @Get annotation. Apart from this we have a HelloServer class that exposes HelloWorldResource class as a restful webservice and then we have restlet client class called Hello Client to consume our web service.
Our app’s directory structure is as below.

restlet-hello-world.png

Our pom.xml file is minimal and looks like below
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.bharatonjava</groupId>
	<artifactId>restlet-examples</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>restlet examples</name>
	<description>restlet-examples	</description>

	<dependencies>
		<dependency>
			<groupId>org.restlet.jse</groupId>
			<artifactId>org.restlet</artifactId>
			<version>2.1.0</version>
		</dependency>
	</dependencies>

	<repositories>
		<repository>
			<id>maven-restlet</id>
			<name>Public online Restlet repository</name>
			<url>http://maven.restlet.org</url>
		</repository>
	</repositories>
	
	<build>
		<finalName>${artifact-id}-${version}</finalName>
	</build>
</project>

if you are not familiar with maven you can refer maven section of this blog

HelloServerResource.java

package com.bharatonjava.rest;

import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

public class HelloServerResource extends ServerResource{

	@Get
	public String greet(){
		return "Hello Bharat! here goes rest!";
	}
	
	@Delete
	public void deleteSomething(){
		System.out.println("server delete method called.");
	}
}

In RESTful terms above class is called a restful resource class. @Get and @Delete annotations in above represent HTTP GET and DELETE methods. Name of your methods can be anything as it doesn’t override your superclass ServerResource methods.

HelloServer.java

package com.bharatonjava.rest;

import org.restlet.Server;
import org.restlet.data.Protocol;

public class HelloServer {

	public static void main(String[] args) throws Exception{
		
		Server server = new Server(Protocol.HTTP,8080,HelloServerResource.class);
		server.start();
	}
}

The main() method in above class creates an HTTP server connector, which is an instance of the Server
class available in the org.restlet package. It’s configured with the protocol, the listening socket port, and the target Restlet that will handle the requests—in this case, the HelloServerResource class.

After launching this Java program, you can point your web browser to the http://localhost:8080 URI and get this line of text: “Hello Bharat! here goes rest!”

HelloClient.java

package com.bharatonjava.rest;

import org.restlet.resource.ClientResource;

public class HelloClient {

	public static void main(String[] args) throws Exception{
		
		ClientResource client = new ClientResource("http://localhost:8080/");
		client.get().write(System.out);

		// client.delete();
	}
}

Restlet not only is a server-side framework, but also gives you the ability to write clients that consume and manipulate resources exposed on the web. On running above client code in your ide you will see “Hello Bharat! here goes rest!” string on your console.

Source code download

Download sourcecode of restlet hello world example

References

http://www.restlet.org/

Advertisements

, , ,

  1. #1 by Amit on September 2, 2013 - 3:11 pm

    thanks for this super simple example..
    Can you do the same example with some pojo as return instead of just plain String?
    I am new to rest and tried this example with simple pojo as return but its just not happening correctly. my pojo is serializable.

  2. #2 by Sitthykun on November 13, 2013 - 7:19 pm

    Nice article!!

  3. #3 by RHL on December 24, 2013 - 1:24 pm

    Can the same be done using Apache Commons HttpClient ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: