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.


Our pom.xml file is minimal and looks like below

<project xmlns="" xmlns:xsi=""
	<name>restlet examples</name>
	<description>restlet-examples	</description>


			<name>Public online Restlet repository</name>

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


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

public class HelloServerResource extends ServerResource{

	public String greet(){
		return "Hello Bharat! here goes rest!";
	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.


import org.restlet.Server;

public class HelloServer {

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

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!”


import org.restlet.resource.ClientResource;

public class HelloClient {

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

		// 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



, , ,

  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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: